APP 中可能会用到某个按钮的长按和短按的功能,以下为 Swift 4 的简单实现。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
| @IBOutlet var button: UIButton!
override func viewDidLoad() { super.viewDidLoad()
let tapGesture = UITapGestureRecognizer(target: self, action: #selector(normalTap(_:))) tapGesture.numberOfTapsRequired = 1 button.addGestureRecognizer(tapGesture) let longGesture = UILongPressGestureRecognizer(target: self, action: #selector(longTap(_:))) button.addGestureRecognizer(longGesture) }
@objc func normalTap(_ sender: UIGestureRecognizer){ }
@objc func longTap(_ sender: UIGestureRecognizer){ if sender.state == .ended { } else if sender.state == .began { } }
|