I have an UILongPressGestureRecognizer
attached to the controller's view
. I want to freeze some timers until user holds his finger. The problem is I can't determine when the touch event is ended. Maybe I should use observer on the gesture recognizer's property state
? Or there are other ways to do this?
Brief
On the controller's view
a UIScrollView
(which implements a paged gallery) is placed, pages can be switched by dragging (swiping). Also there is an UITapGestureRecognizer
, also attached to the controller's view
, which handles some other tasks.
Yes you can accomplish this by looking at the recognizer's state, but you don't need to use an observer. You should declare an action method in your gesture recognizer's delegate that will be called when the recognizer fires. The method will then automatically be called whenever the recognizer's state changes.
You need to look for the state UIGestureRecognizerStateBegan to begin your timer, and you need to look for the states UIGestureRecognizerStateEnded, UIGestureRecognizerStateFailed, and UIGestureRecognizerStateCancelled to pause your timer.
Simply connect up your gesture with the action in Interface Builder.
-(IBAction)longPressBegan:(UILongPressGestureRecognizer *)recognizer
{
if (recognizer.state == UIGestureRecognizerStateBegan)
{
// Long press detected, start the timer
}
else
{
if (recognizer.state == UIGestureRecognizerStateCancelled
|| recognizer.state == UIGestureRecognizerStateFailed
|| recognizer.state == UIGestureRecognizerStateEnded)
{
// Long press ended, stop the timer
}
}
}