在满足特定条件时检测LongPress

时间:2016-07-12 05:55:51

标签: ios uilongpressgesturerecogni

我在我的集​​合视图中使用UILongPressGestureRecognizer,我希望长按手势识别器只有在满足某些条件时才能工作。

NSString *check;
if([check isEqualToString:@"Enabled"]
{
    //long press should be detected. or following method should be called
}

-(void)handleLongPressGesture:(UILongPressGestureRecognizer *)gestureRecognizer

{
}

2 个答案:

答案 0 :(得分:1)

添加UIGestureRecognizerDelegate

- (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer{

    NSString *check;
    if([check isEqualToString:@"Enabled"]
    {
        //long press should be detected. or following method should be called
        return YES;
    }else{
        return NO;
    }
}

答案 1 :(得分:1)

NSString *check;
UILongPressGestureRecognizer *longPress =[ [UILongPressGestureRecognizer alloc]init];

if([check isEqualToString:@"Enabled"]
{
UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc]
                                         initWithTarget:self 
                                         action:@selector(handleLongPressGesture:)];
}else{

}

-(void)handleLongPressGesture:(UILongPressGestureRecognizer *)gestureRecognizer

{

}