UIButton long press with finger stationary

前端 未结 2 1078
别跟我提以往
别跟我提以往 2020-12-20 03:55

In my project I have the need to use a UIButton (or another component) to handle events using long press. Let me explain, I should make that mind I hold down the button a ti

相关标签:
2条回答
  • 2020-12-20 04:10

    Use these two methods for buttons events. touchDown is called when you press the button and touchUp will be called when you lift your finger from the button. Calculate the time difference between these two methods. Also you can start timer in touchDown and stop/restart it in touchUp.

    //connect this action with Touch up inside
    - (IBAction)touchUp:(id)sender {
        NSLog(@"up");
    }
    
    //connect this to tocuh down
    - (IBAction)touchDown:(id)sender{
        NSLog(@"down");
    }
    

    Updated In coding you can write like this

    [btn addTarget:self action:@selector(touchUp:) forControlEvents:UIControlEventTouchUpInside];
    [btn addTarget:self action:@selector(touchDown:) forControlEvents:UIControlEventTouchDown];
    

    and in xib enter image description here

    0 讨论(0)
  • 2020-12-20 04:32

    The same I have done...As u said about UILongPressGestureRecognizer, I can't understand it..but u can write ur code inside-(void)handleLongPress:(UILongPressGestureRecognizer *)gestureRecognizer { }. I have done same by using this method and got successful result..:). You even don't need to add timer,instead u can use...

     UILongPressGestureRecognizer *lpgr = [[UILongPressGestureRecognizer alloc] 
                                          initWithTarget:self action:@selector(handleLongPress:)];
    lpgr.minimumPressDuration = 1.0;
    lpgr.delegate = self;
    

    I think this works perfectly..

    0 讨论(0)
提交回复
热议问题