detecting long tap on iPhone

前端 未结 3 2131
后悔当初
后悔当初 2021-02-06 07:17

I am working on an iPhone app which requires me to check if the button has been tapped & held pressed for 6 seconds & then fire an action which is playing some sort of s

3条回答
  •  一生所求
    2021-02-06 08:09

    I realize this is quite dated question, however answer should be pretty simple.

    In your View controller viewDidLoad:

    //create long press gesture recognizer(gestureHandler will be triggered after gesture is detected)
    UILongPressGestureRecognizer* longPressGesture = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(gestureHandler:)];
    //adjust time interval(floating value CFTimeInterval in seconds)
    [longPressGesture setMinimumPressDuration:6.0];
    //add gesture to view you want to listen for it(note that if you want whole view to "listen" for gestures you should add gesture to self.view instead)
    [self.m_pTable addGestureRecognizer:longPressGesture];
    [longPressGesture release];
    

    Then in your gestureHandler:

    -(void)gestureHandler:(UISwipeGestureRecognizer *)gesture
    {
        if(UIGestureRecognizerStateBegan == gesture.state)
        {//your code here
    
        /*uncomment this to get which exact row was long pressed
        CGPoint location = [gesture locationInView:self.m_pTable];
        NSIndexPath *swipedIndexPath = [self.m_pTable indexPathForRowAtPoint:location];*/
        }
    }
    

提交回复
热议问题