双击UIButton
如何识别UIButton的双击?
为控件事件UIControlEventTouchDownRepeat添加一个目标动作,并仅在触摸的tapCount
为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评论说的,双击一个button是一种不寻常的手势。 在iPhone OS中,双击主要用于缩放视图以放大/缩小焦点区域。 如果您使手势执行其他操作,则对用户来说可能是不直观的。
Swift 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. } }
@IBOutlet weak var button: UIButton! override func viewDidLoad() { super.viewDidLoad() button.addTarget(self, action: "didTap:", forControlEvents: .TouchUpInside) button.addTarget(self, action: "didDoubleTap:", forControlEvents: .TouchDownRepeat) } var ignoreTap = false func didTap(sender: UIButton) { if ignoreTap { ignoreTap = false print("ignoretap", sender) return } print("didTap", sender) } func didDoubleTap(sender: UIButton) { ignoreTap = true print("didDoubleTap", sender) }
尝试使用这个button事件
UIControlEventTouchDownRepeat