如果您想通过单击来执行任何动作并长按,则可以通过以下方式将手势添加到按钮中:
@IBOutlet weak var btn: UIButton!override func viewDidLoad() { let tapGesture = UITapGestureRecognizer(target: self, #selector (tap)) //Tap function will call when user tap on button let longGesture = UILongPressGestureRecognizer(target: self, #selector(long)) //Long function will call when user long press on button. tapGesture.numberOfTapsRequired = 1 btn.addGestureRecognizer(tapGesture) btn.addGestureRecognizer(longGesture)}@objc func tap() { print("Tap happend")}@objc func long() { print("Long press")}
这样,您可以为单个按钮添加多种方法,而您只需要为此按钮设置插座即可。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)