我在我的UICollectionView
中使用了UILongPressGestureRecognizer
。現在我的時候我一定的時間後,握住我的的CollectionView項目手指(例如1秒),我希望我的UILongPressGestureRecognizer
結束和執行一定的代碼:在某段時間後取消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秒後發生嗎? – 2015-02-09 17:48:44
@LyndseyScott我希望當我的手指按住收集單元后3秒的結束手勢... – kasiri182 2015-02-10 06:08:30
如果你想'minimumPressDuration'和最大持續時間爲3,你可以簡單地把你的結束代碼放在一個'if(gestureRecognizer .state == UIGestureRecognizerStateBegan){'conditional。 – 2015-02-10 06:11:01