2011-05-18 39 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:確定哪個按鈕在按下的手指下。

相關問題