How to get functionality of Long Press gesture in iOS below 3.2

心已入冬 提交于 2019-12-20 22:56:30

问题


UILongPressGesture is available in ios ver 3.2 and later. But i am trying to develop application for maximum compatibility and hence targeting ios ver2.0

Can anyone please guide me on how to accomplish long press gesture in ios v2.0


回答1:


For a single finger, it's pretty simple: Start a timer in the touchesBegan method and trigger an action when the timer fires. Cancel the timer if you get a touchesEnded before it fires. Here's an implementation that uses the performSelector:withObject:afterDelay: method.

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    [self performSelector:@selector(fireLongPress)
               withObject:nil
               afterDelay:LONG_PRESS_THRESHOLD];
}

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
    [NSObject cancelPreviousPerformRequestsWithTarget:self];
}

- (void)fireLongPress {
    // do what you want to do
}

You'll probably also want to kill the timer if the finger moves too far.

With multitouch, it's a bit more complicated. You'll have to keep track of which touch is which, and decide what to do e.g. when one finger has pressed long enough but the other hasn't (or figure out what UILongPressGestureRecognizer does).




回答2:


Implement the touches... methods in your view. If a certain amount of time passes between touchesBegan:withEvent: and touchesEnded:withEvent: without any touchesMoved:withEvent: events, you have a long press.



来源:https://stackoverflow.com/questions/4565889/how-to-get-functionality-of-long-press-gesture-in-ios-below-3-2

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!