我无法理解为什么要设置
longGesture.minimumPressDuration = 0.3;
longGesture.delaysTouchesBegan = true;
在action
被解雇之前,你知道为什么吗?
longGesture = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(handleGesture:)];
longGesture.minimumPressDuration = 0.3;
longGesture.delaysTouchesBegan = true;
[self.collectionView addGestureRecognizer:longGesture];
答案 0 :(得分:0)
Try this code. You don't have to set this longGesture.minimumPressDuration = 0.3
- (void)viewDidLoad
{
UILongPressGestureRecognizer *lpgr
= [[UILongPressGestureRecognizer alloc]
initWithTarget:self action:@selector(handleLongPress:)];
lpgr.delegate = self;
lpgr.delaysTouchesBegan = YES;
[self.collectionView addGestureRecognizer:lpgr];
}
-(void)handleLongPress:(UILongPressGestureRecognizer *)gestureRecognizer
{
if (gestureRecognizer.state != UIGestureRecognizerStateEnded) {
return;
}
CGPoint p = [gestureRecognizer locationInView:self.collectionView];
NSIndexPath *indexPath = [self.collectionView indexPathForItemAtPoint:p];
if (indexPath == nil){
NSLog(@"couldn't find index path");
} else {
// get the cell at indexPath (the one you long pressed)
UICollectionViewCell* cell =
[self.collectionView cellForItemAtIndexPath:indexPath];
// do stuff with the cell`enter code here`
}
}