本文介绍了双击UIButton的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何识别UIButton上的双击?

How to recognize double touch on UIButton ?

推荐答案

为控件事件UIControlEventTouchDownRepeat添加目标动作,仅在触摸的tapCount为2时执行动作.

Add an target-action for the control event UIControlEventTouchDownRepeat, and do action only when the touch's tapCount is 2.

Objective-C:

[button addTarget:self action:@selector(multipleTap:withEvent:)
             forControlEvents:UIControlEventTouchDownRepeat];

...

-(IBAction)multipleTap:(id)sender withEvent:(UIEvent*)event {
   UITouch* touch = [[event allTouches] anyObject];
   if (touch.tapCount == 2) {
     // do action.
   }
}

正如@Gavin所评论的那样,连按两次按钮是一种不寻常的手势.在iPhone OS上,双击通常用于可缩放视图,以放大/缩小焦点区域.如果您做出手势来执行其他操作,对于用户来说可能是不直观的.

快捷键3:

button.addTarget(self, action: #selector(multipleTap(_:event:)), for: UIControlEvents.touchDownRepeat)

然后:

func multipleTap(_ sender: UIButton, event: UIEvent) {
    let touch: UITouch = event.allTouches!.first!
    if (touch.tapCount == 2) {
        // do action.
    }
}

这篇关于双击UIButton的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-22 16:42