简体   繁体   English

如何区分点击并按下UIButton iOS

[英]how to differentiate click and press on UIButton iOS

I have one button which i am using to record video and take photo from camera. 我有一个按钮,我用来录制视频和从相机拍照。

On click i want to take picture and on press start video recording. 点击我想拍照并按下开始视频录制。

      [self.recordButton addTarget:self action:@selector(recordTouchCancel:) forControlEvents:UIControlEventTouchCancel];
  [self.recordButton addTarget:self action:@selector(recordTouchDown:) forControlEvents:UIControlEventTouchDown];
  [self.recordButton addTarget:self action:@selector(recordTouchUp:) forControlEvents:UIControlEventTouchUpInside];
  [self.recordButton addTarget:self action:@selector(recordTouchUp:) forControlEvents:UIControlEventTouchUpOutside];

Press to record is working perfect but not able to get click, how can i differentiate click and hold on UIButton. 按录制工作完美但无法点击,如何区分点击并按住UIButton。

Any suggestions ? 有什么建议么 ?

I think you might looking for UILongPressGestureRecognizer . 我想你可能会寻找UILongPressGestureRecognizer I notice that the API automatically trigger the right event between the tap and long press. 我注意到API在点击和长按之间自动触发正确的事件。

Take a look at Apple doc about https://developer.apple.com/library/prerelease/ios/documentation/UIKit/Reference/UILongPressGestureRecognizer_Class/index.html 看一下关于 https://developer.apple.com/library/prerelease/ios/documentation/UIKit/Reference/UILongPressGestureRecognizer_Class/index.html的 Apple文档

There is an exemple : 有一个例子

- (void)viewDidLoad {
    // FRIST SET TARGET & SELECTOR TO BUTTON
    UILongPressGestureRecognizer * pressLong =  [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(takeVideo:)];
    [self.recordButton addGestureRecognizer:pressLong];
    [self.recordButton addTarget:self action:@selector(takePicture:) forControlEvents:UIControlEventTouchUpInside];
}


-(void)takePicture:(UIButton*) sender {
    // TAKE PIC
}


-(void)takeVideo : (UILongPressGestureRecognizer*) gesture   {

    NSInteger state = gesture.state;

    switch (state) {
        case UIGestureRecognizerStateChanged: break;
        case UIGestureRecognizerStateBegan:
             //START RECORD
            break;
        case UIGestureRecognizerStateEnded:
             //END RECORD
            break;
    }

}

"UIControlEventTouchDown" and "UIControlEventTouchUpInside" are 2 different state. “UIControlEventTouchDown”和“UIControlEventTouchUpInside”是两种不同的状态。 when you start to tab button "UIControlEventTouchDown" is called and when you give you finger "UIControlEventTouchUpInside" is called.and time between start and end of your tap there is no other state. 当你开始选项卡按钮“UIControlEventTouchDown”被调用时,当你给你手指“UIControlEventTouchUpInside”被调用时,你的点击开始和结束之间的时间没有其他状态。 So you can't do by this way. 所以你不能这样做。

But you can try "UIControlEventTouchDownRepeat" state. 但是你可以尝试“UIControlEventTouchDownRepeat”状态。 this is called when user tap twice on a button. 当用户在按钮上点击两次时调用此方法。

In you situation it better to use "tapGuesture" 在你的情况下,最好使用“tapGuesture”

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM