今天遇到一個問題。就是給了個長按手勢,然後調用方法,但是總是調用兩次,第二次調用,會使我的方法崩潰,找了好久才找到。
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的狀態