2011-05-18 33 views
10

我想创建一个应用程序,在UILongPressGestureRecognizer手势被触发时可以拖放UIButton。UILongPressGestureRecognizer

我:

UIGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc] 
initWithTarget:self action:@selector(handleLongPress:)]; 

而且

- (void)handleLongPress:(UILongPressGestureRecognizer *)recognizer { 
    CGPoint location = [recognizer locationInView:self.view]; 

    switch (recognizer.state) { 
     case UIGestureRecognizerStateBegan: 
      //NSLog(@"handleLongPress: StateBegan"); 
      break; 
     case UIGestureRecognizerStateChanged: 
      if(location.y > 75.0 && location.x > 25 && location.x < 300) 
       button.frame = CGRectMake(location.x-25, location.y-15, 50, 30);   
      break; 
     case UIGestureRecognizerStateEnded: 
      //NSLog(@"handleLongPress: StateEnded"); 
      break; 
     default: 
      break; 
    } 
} 

这一个按钮(即伊娃button)的伟大工程。我怎样才能向handleLongPress函数发送正在按下的当前按钮?换句话说,我愿做类似下面,我在sender

- (void)handleLongPress:(UILongPressGestureRecognizer *)recognizer:(id)sender { 
    CGPoint location = [recognizer locationInView:self.view]; 

    switch (recognizer.state) { 
     case UIGestureRecognizerStateBegan: 
      //NSLog(@"handleLongPress: StateBegan"); 
      break; 
     case UIGestureRecognizerStateChanged: 
      if(location.y > 75.0 && location.x > 25 && location.x < 300) 
       sender.frame = CGRectMake(location.x-25, location.y-15, 50, 30);   
      break; 
     case UIGestureRecognizerStateEnded: 
      //NSLog(@"handleLongPress: StateEnded"); 
      break; 
     default: 
      break; 
    } 
} 

回答

12

你试过recognizer.view

+0

工作。谢谢! – v0idless 2011-05-18 06:15:39

+0

@ v0idless:欢迎。 – EmptyStack 2011-05-18 06:52:40

-2

通过你会通过你所有的按钮进入handleLongPress必须循环。 使用locationInView和pointInside:withEvent:确定哪个按钮在按下的手指下。

相关问题