在UIScrollView中检测长按

时间:2010-04-06 14:21:16

标签: iphone cocoa-touch iphone-sdk-3.0

如何在UIScrollView中检测到长按(点击并按住)?

2 个答案:

答案 0 :(得分:9)

在视图的touchesBegan:中,您可以稍微调用“长按”句柄。

[touchHandler performSelector:@selector(longTap:) withObject:nil afterDelay:1.5];

然后在视图的touchesEnded:中,如果时间不够,您可以取消该呼叫:

[NSObject cancelPreviousPerformRequestsWithTarget:touchHandler selector:@selector(longTap:) object:nil];

答案 1 :(得分:2)

//Add  gesture to a method where the view is being created. In this example long tap is added to tile (a subclass of UIView):

    // Add long tap for the main tiles
    UILongPressGestureRecognizer *longPressGesture = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longTap:)];
    [tile addGestureRecognizer:longPressGesture];
    [longPressGesture release];

-(void) longTap:(UILongPressGestureRecognizer *)gestureRecognizer{
    NSLog(@"gestureRecognizer= %@",gestureRecognizer);
    if ([gestureRecognizer state] == UIGestureRecognizerStateBegan) {
        NSLog(@"longTap began");

    }

}