我正在 swift
中创建一个相机应用程序,并且我有一个 UIButton
。我想提出两个选项:当用户单击按钮时 - 它会拍照,当用户将手指放在按钮上时 - 它会录制电影,直到用户释放按钮。
我有录音和拍照功能,现在我需要区分按钮上的用户操作。
此按钮的可用操作是:
并且我尝试在touch down
开始录制并在touch up inside
停止录制,但是我不知道我应该把负责拍摄的代码放在哪里相片。如果我也把它放在 touch down
中,那么当用户开始录制电影时 - 也会拍照,我想避免它。
最佳答案
点击和长按的手势识别器可以很好地相互配合以缩短这一点(点击延迟触发,直到确定它不是长按)。
class ViewController: UIViewController{
@IBOutlet weak var button: UIButton!
override func viewDidLoad() {
super.viewDidLoad()
button.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(tap)))
let longPressGestureRecognizer = UILongPressGestureRecognizer(target: self, action: #selector(longPress))
longPressGestureRecognizer.minimumPressDuration = 1
button.addGestureRecognizer(longPressGestureRecognizer)
}
@objc private func tap(tapGestureRecognizer: UITapGestureRecognizer) {
print("tap")
}
@objc private func longPress (longPressGestureRecognizer: UILongPressGestureRecognizer) {
if longPressGestureRecognizer.state == .began {
print("long press began")
}
}
}
关于ios - 如何区分用户是快速点击 UIButton 还是在 Swift 中放置并按住它?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40370640/