开发者

How to Recognize a Hold Button {iPhone SDK}

Hi i ha开发者_如何转开发ve a Button i want hold this button to write something but i don't know how can i recognize hold button , can you help me ? thank you


TouchDownInside event triggered, start a NStimer. TouchUpInside event triggered, cancel the timer. Make the timer call your method to execute if the user holds the button : the timer delay will be the amount of time required to recognize hold.


You can also use UILongPressGestureRecognizer.

In your initialization method (e.g. viewDidLoad), create a gesture recognizer and attach it to your button:

UILongPressGestureRecognizer *gesture = [[UILongPressGestureRecognizer alloc] 
  initWithTarget:self 
  action:@selector(myButtonLongPressed:)];
// you can control how many seconds before the gesture is recognized  
gesture.minimumPressDuration = 2; 
// attach the gesture to your button
[myButton addGestureRecognizer:gesture];
[gesture release];

The event handler myButtonLongPressed: should look like this:

- (void) myButtonLongPressed:(UILongPressGestureRecognizer *)gesture
{
  // Button was long pressed, do something
}

Note that UILongPressGestureRecognizer is a continuous event recognizer. While the user is still holding down the button, myButtonLongPressed: will be called multiple times. If you just want to handle the first call, you can check the state in myButtonLongPressed::

if (gesture.state == UIGestureRecognizerStateBegan) {
  // Button was long pressed, do something
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜