1

我已经在 ViewController 中实现了一个可拖动的 UIButton。除了这个可拖动的按钮之外,我还有两个 UIButtons,当我在它们上面拖动一个按钮并更改静态按钮的标题时,它们应该被检测到。我该如何实现这一点?

这就是可拖动按钮的实现方式。

@interface ButtonAnimationViewController : UIViewController

@property (weak, nonatomic) IBOutlet UIButton *firstButton; // dragable button
@property (weak, nonatomic) IBOutlet UIButton *oneButton;   //normal button
@property (weak, nonatomic) IBOutlet UIButton *twoButton;   // normal button


@implementation ButtonAnimationViewController

 - (void)viewDidLoad
{
 [super viewDidLoad];
 [self.view addSubview:self.firstButton];
 [self.view addSubview:self.oneButton];
 [self.view addSubview:self.twoButton];

UIPanGestureRecognizer *panGesture 
    = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(dragging:)];
[self.firstButton addGestureRecognizer:panGesture];

UITapGestureRecognizer *tapGesture 
    = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(dropping:)];
tapGesture.cancelsTouchesInView = NO;
[self.oneButton addGestureRecognizer:tapGesture];
[self.twoButton addGestureRecognizer:tapGesture];


 -(void)dragging:(UIPanGestureRecognizer*)panGesture {

  if (panGesture.view != self.firstButton)
  {
    return;
  }
   if (panGesture.state == UIGestureRecognizerStateBegan || 
       panGesture.state == UIGestureRecognizerStateChanged)
  {
    CGPoint delta = [panGesture translationInView:self.view];
    CGPoint center = self.firstButton.center;
    center.x += delta.x;
    center.y += delta.y;
    self.firstButton.center = center;
    [panGesture setTranslation:CGPointZero inView:self.view];
  }
    if (panGesture.view == self.oneButton) {
    // I tried this to change the button title.
    NSString *buttonTitle = self.firstButton.titleLabel.text; 
    self.oneButton.titleLabel.text = buttonTitle;
    return;
}
    //if (panGesture.state == UIGestureRecognizerStateEnded) {
   // self.firstButton.center = center;                        
    //[panGesture setTranslation:CGPointZero inView:self.view];
}
4

2 回答 2

1

诀窍是使用CGRectIntersectsRect .. 这是了解一个视图是否与另一个视图相交的基本构建块。在此处查看使用该功能的相关示例。

这是示例代码:

  if (panGesture.state == UIGestureRecognizerStateBegan || 
       panGesture.state == UIGestureRecognizerStateChanged)
  {
    CGPoint delta = [panGesture translationInView:self.view];
    CGPoint center = self.firstButton.center;
    center.x += delta.x;
    center.y += delta.y;
    self.firstButton.center = center;
    [panGesture setTranslation:CGPointZero inView:self.view];
  }

  // check if there is an overlap
  NSString *draggableButtonTitle = self.firstButton.titleLabel.text;
  if (CGRectIntersectsRect(self.firstButton.frame, self.oneButton.frame)) {
      self.oneButton.titleLabel.text = draggableButtonTitle;
      // i donno if you wanna return here or continue dragging.. up to you
  } else if (CGRectIntersectsRect(self.firstButton.frame, self.twoButton.frame)) {
      self.twoButton.titleLabel.text = draggableButtonTitle;
  }
于 2013-09-27T13:35:13.890 回答
0
- (void)handlePan:(UIPanGestureRecognizer *)gesture
{
    if (gesture.state == UIGestureRecognizerStateChanged) {
        CGPoint transition = [gesture translationInView:self.view];
        self.dragableButton.center = CGPointMake(self.dragableButton.center.x + transition.x, self.dragableButton.center.y + transition.y);

        CGPoint touchPoint = [gesture locationInView:self.view];
        if (CGRectContainsPoint(self.hoverButtonA.frame, touchPoint)) {
            self.hoverButtonA.alpha = 0.2;
            //change the title here?
        }
        else {
            self.hoverButtonA.alpha = 1;
            //change the title back to the origin?
        }

        //Same for the other button
        if (CGRectContainsPoint(self.hoverButtonB.frame, touchPoint)) {
            self.hoverButtonB.alpha = 0.2;
        }
        else {
            self.hoverButtonB.alpha = 1;
        }

        [gesture setTranslation:CGPointZero inView:self.view];
    }
    //I'll leave out the end part, it pretty much just like the implementation above.
}

只添加了gestureRecognizer dragableButton,为什么会有两个tapGesture,而不是使用target action呢?此外,One GestureRecognizer 可以而且只能有一个视图。将其添加到多个视图将不起作用。

于 2013-09-27T15:11:25.407 回答