Ios 在特定时间后取消UILongPressGestureRecognitor
我在我的Ios 在特定时间后取消UILongPressGestureRecognitor,ios,objective-c,uicollectionview,uigesturerecognizer,Ios,Objective C,Uicollectionview,Uigesturerecognizer,我在我的UICollectionView中使用了uiLongPressGestureRecognitor。现在,当我在一定时间后(例如1秒)用手指触摸CollectionView项目时,我希望我的ui长按gestureRecognitor结束并执行特定代码: if (gestureRecognizer.state == UIGestureRecognizerStateEnded) {} 这是我的代码: - (void)viewDidLoad { [super viewDidLoad];
UICollectionView
中使用了uiLongPressGestureRecognitor
。现在,当我在一定时间后(例如1秒)用手指触摸CollectionView项目时,我希望我的ui长按gestureRecognitor
结束并执行特定代码:
if (gestureRecognizer.state == UIGestureRecognizerStateEnded) {}
这是我的代码:
- (void)viewDidLoad {
[super viewDidLoad];
Public = [[PublicMethods alloc]init];
self.view.backgroundColor = [UIColor whiteColor];
[self.view addSubview:self.collect];
UILongPressGestureRecognizer *lpgr = [[UILongPressGestureRecognizer alloc]initWithTarget:self action:@selector(handleLongPress:)];
lpgr.minimumPressDuration = 3; //seconds
lpgr.delaysTouchesBegan = YES;
lpgr.delegate = self;
[self.collect addGestureRecognizer:lpgr];
}
-(void)handleLongPress:(UILongPressGestureRecognizer *)gestureRecognizer {
if (gestureRecognizer.state != UIGestureRecognizerStateEnded) {
return;
}
CGPoint p = [gestureRecognizer locationInView:self.collect];
NSIndexPath *indexPath = [self.collect indexPathForItemAtPoint:p];
if (indexPath == nil){
NSLog(@"couldn't find index path");
} else {
// get the cell at indexPath (the one you long pressed)
//CollectionViewCell* cell = (CollectionViewCell*)[self.collect cellForItemAtIndexPath:indexPath];
UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"bala" message:@"jalaaaa" delegate:self cancelButtonTitle:@"ok" otherButtonTitles:nil, nil];
[alert show];
}
}
您可以在启动
ui长按手势识别器时实例化计时器,然后在计时器完成后取消手势并执行“结束手势”代码,例如(使用1秒的时间限制):
嘿,你想让它发生在你的minimumPressDuration=3秒后的1秒还是3秒后?@LyndseyScott我想在3秒结束手势后将我的手指放在采集单元上……如果你想让minimumPressDuration
和最大持续时间都是3秒,你可以简单地将结束代码放在如果(gestureRecognizer.state==UIGestureRecognizerStateStart){
有条件。我当前的回答假设您希望手势在识别后3秒取消,例如,如果手势在用户开始按屏幕后3秒开始,它将在6秒钟内取消。FYI@LyndseyScott我的朋友你的答案不起作用!!!!我的朋友这个答案不起作用。我得到这个按摩“找不到索引路径”@mamal10那行代码对你有用吗?我根本没有更改过你的那部分代码。@mamal10如果它以前确实对你有用,请查看我的更新答案,因为它可能会有所不同。我移动了手势取消代码,所以它在你的“结束手势”之后“代码。我的朋友,我想在3秒钟结束手势后,即使我把手指放在手机上,我也要这样做。”cell@mamal10然后(就像我在各种评论中提到的)更改minimumPressDuration和timer duration,使minimumPressDuration+timer duration==3,以便自动识别手势,或者只需将结束代码放入一个if(gestureRecognitizer.state==uigestureRecognitizerStateStart){conditional
,以便在3秒时识别手势(minimumPressDuration),代码立即执行。
- (void)handleLongPress:(UILongPressGestureRecognizer *)gestureRecognizer {
if (gestureRecognizer.state == UIGestureRecognizerStateBegan) {
// Create a timer that calls cancel: 2.5 second after the
// gesture begins (i.e. 3 seconds after the button press if
// if lpgr.minimumPressDuration = .5;. Pass the gesture
// recognizer along within the user info dictionary parameter.
timer = [NSTimer scheduledTimerWithTimeInterval:2.5 target:self selector:@selector(cancel:) userInfo:[NSDictionary dictionaryWithObjectsAndKeys:gestureRecognizer, @"gestureRecognizer", nil] repeats:NO];
} else if (gestureRecognizer.state == UIGestureRecognizerStateEnded) {
// Assuming you still want to execute the end code
// if the user lifts their finger before the 3 seconds
// is complete, use the same method called in the timer.
[self cancel:nil];
}
}
- (void)cancel:(NSTimer*)timerObject {
NSLog(@"%@",[timer.userInfo objectForKey:@"gestureRecognizer"]);
// Get the gesture recognizer from the info dictionary
UIGestureRecognizer *gestureRecognizer = [timer.userInfo objectForKey:@"gestureRecognizer"];
CGPoint p = [gestureRecognizer locationInView:self.collect];
NSIndexPath *indexPath = [self.collect indexPathForItemAtPoint:p];
if (indexPath == nil){
NSLog(@"couldn't find index path");
} else {
// get the cell at indexPath (the one you long pressed)
//CollectionViewCell* cell = (CollectionViewCell*)[self.collect cellForItemAtIndexPath:indexPath];
UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"bala" message:@"jalaaaa" delegate:self cancelButtonTitle:@"ok" otherButtonTitles:nil, nil];
[alert show];
}
// Disable it and re-enable it to cancel the gesture
gestureRecognizer.enabled = NO;
gestureRecognizer.enabled = YES;
// Invalidate the timer
[timer invalidate];
timer = nil;
}