2016-07-12 77 views
0

我在我的收藏視圖中使用UILongPressGestureRecognizer,我希望長按手勢識別器只有在滿足某些條件時才應起作用。當滿足某些條件時檢測LongPress

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

-(void)handleLongPressGesture:(UILongPressGestureRecognizer *)gestureRecognizer 

{ 
} 

回答

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; 
    } 
} 
+0

'shouldRecognizeSimultaneouslyWithGestureRecognizer'不能被用於此目的。 gestureRecognizerShouldBegin是正確的。 –

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 

{ 

} 
相關問題