Welcome toVigges Developer Community-Open, Learning,Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
974 views
in Technique[技术] by (71.8m points)

iphone - UIButton long press with finger stationary

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 timer to count the seconds and release to pressure stop, I tried with the management of UILongPressGestureRecognizer but is not the case because I recall the event when the button is held down but only if I move my finger, but I wish the timer went away and counted all the time in which the button is held down (with your finger stationary) and stopped counting when the finger is released.

Does anyone know how to help me? Thanks

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to Vigges Developer Community for programmer and developer-Open, Learning and Share
...