我用UILongPressGestureRecognizer我的UICollectionView.现在,当我在一段时间(例如1秒)后将手指放在CollectionView项目上时,我希望我UILongPressGestureRecognizer结束并执行某个代码:
if (gestureRecognizer.state == UIGestureRecognizerStateEnded) {}
Run Code Online (Sandbox Code Playgroud)
这是我的代码:
- (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 …Run Code Online (Sandbox Code Playgroud)