2015-02-09 48 views
3

我在我的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]; 
    } 
} 
+0

嘿,你想讓它在自你的minimumPressDuration = 3以來的1秒或3秒後發生嗎? – 2015-02-09 17:48:44

+0

@LyndseyScott我希望當我的手指按住收集單元后3秒的結束手勢... – kasiri182 2015-02-10 06:08:30

+0

如果你想'minimumPressDuration'和最大持續時間爲3,你可以簡單地把你的結束代碼放在一個'if(gestureRecognizer .state == UIGestureRecognizerStateBegan){'conditional。 – 2015-02-10 06:11:01

回答

7

你可以在所述UILongPressGestureRecognizer開始實例化一個計時器然後取消該手勢,並且一旦定時器完成時,前執行「結束手勢」代碼(使用1秒的時間限制):

- (void)handleLongPress:(UILongPressGestureRecognizer *)gestureRecognizer { 
    if (gestureRecognizer.state == UIGestureRecognizerStateBegan) { 
     // Create a timer that calls cancel: 2.5 second after the 
     // gesture begins (i.e. 3 seconds after the button press if 
     // if lpgr.minimumPressDuration = .5;. Pass the gesture 
     // recognizer along within the user info dictionary parameter. 
     timer = [NSTimer scheduledTimerWithTimeInterval:2.5 target:self selector:@selector(cancel:) userInfo:[NSDictionary dictionaryWithObjectsAndKeys:gestureRecognizer, @"gestureRecognizer", nil] repeats:NO]; 

    } else if (gestureRecognizer.state == UIGestureRecognizerStateEnded) { 
     // Assuming you still want to execute the end code 
     // if the user lifts their finger before the 3 seconds 
     // is complete, use the same method called in the timer. 
     [self cancel:nil]; 
    } 
} 

- (void)cancel:(NSTimer*)timerObject { 

    NSLog(@"%@",[timer.userInfo objectForKey:@"gestureRecognizer"]); 
    // Get the gesture recognizer from the info dictionary 
    UIGestureRecognizer *gestureRecognizer = [timer.userInfo objectForKey:@"gestureRecognizer"]; 

    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]; 
    } 

    // Disable it and re-enable it to cancel the gesture 
    gestureRecognizer.enabled = NO; 
    gestureRecognizer.enabled = YES; 

    // Invalidate the timer 
    [timer invalidate]; 
    timer = nil; 
} 
+0

我的朋友這個答案不起作用。我得到這個按摩「找不到索引路徑」 – kasiri182 2015-02-10 06:45:56

+0

@ mamal10是否有線代碼爲你工作?我根本沒有改變你的代碼的一部分。 – 2015-02-10 06:47:06

+0

@ mamal10如果事實上它爲你工作之前,請查看我的更新答案,因爲它可能會有所作爲。我已經移動了手勢取消代碼,因此它是在您的「結束手勢」代碼之後。 – 2015-02-10 06:48:41

相關問題