今天遇到一个问题。就是给了个长按手势,然后调用方法,但是总是调用两次,第二次调用,会使我的方法崩溃,找了好久才找到。
UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(handleLongPressGuesture:)];
longPress.minimumPressDuration = 1.0;
longPress.delegate = self;
longPress.delaysTouchesBegan = YES;
longPress.numberOfTouchesRequired = 1;
[_collectionView addGestureRecognizer:longPress];
- (void)handleLongPressGuesture:(UILongPressGestureRecognizer *)guesture{
if (guesture.state == UIGestureRecognizerStateBegan) {
NSLog(@"开始");
CGPoint point = [guesture locationInView:_collectionView];
NSIndexPath *indexpath = [_collectionView indexPathForItemAtPoint:point];
UIPasteboard *pasteboard = [UIPasteboard generalPasteboard];
// NSString *tempStr = [self.dataSouce[indexpath.section - 1][indexpath.row] objectForKey:@"imgUrl"];
NSString *tempStr = [self.dataSouce[indexpath.section - 1][indexpath.row] objectForKey:@"imgUrl"];
NSLog(@"打印内容%@",tempStr);
pasteboard.string = tempStr;
if (pasteboard.string.length)
{
[KyoUtil showAlertWithString:@"复制成功"];
}else{
[KyoUtil showAlertWithString:@"复制失败"];
}
}else{
NSLog(@"结束");
}
}
解决方法就是在长按方法里面,判断touch的状态