【发布时间】:2015-02-09 16:38:39
【问题描述】:
我在UICollectionView 中使用了UILongPressGestureRecognizer。现在,当我在一定时间(例如 1 秒)后将手指放在 CollectionView 项目上时,我希望我的 UILongPressGestureRecognizer 结束并执行特定代码:
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];
}
}
【问题讨论】:
-
嘿,您希望它在您的 minimumPressDuration = 3 后 1 秒或 3 秒后发生吗?
-
@LyndseyScott 我希望在 3 秒结束手势后将手指放在收集单元上...
-
如果您希望
minimumPressDuration和最大持续时间都为 3,您可以简单地将结束代码放在if (gestureRecognizer.state == UIGestureRecognizerStateBegan) {条件中。 -
我当前的答案假设您希望手势在被识别后 3 秒取消,例如,如果手势在用户开始按下屏幕后 3 秒开始,它将在 6 秒内取消。仅供参考
-
@LyndseyScott 我的朋友,你的答案不起作用!!!
标签: ios objective-c uicollectionview uigesturerecognizer