Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/102.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 触地重复事件不起作用_Ios_Swift_Uibutton - Fatal编程技术网

Ios 触地重复事件不起作用

Ios 触地重复事件不起作用,ios,swift,uibutton,Ios,Swift,Uibutton,我想我的按钮改变其背景图像时,用户点击它两次。为此,我使用触地重复事件。然而,它不起作用 button.addTarget(self, action: #selector(clickedRepeatOnPlate), for: .touchDownRepeat) 这使您可以设置双击视图的手势 let tap = UITapGestureRecognizer(target: self, action: #selector(doubleTapped)) tap.numberOfTapsRequir

我想我的按钮改变其背景图像时,用户点击它两次。为此,我使用触地重复事件。然而,它不起作用

button.addTarget(self, action: #selector(clickedRepeatOnPlate), for: .touchDownRepeat)

这使您可以设置双击视图的手势

let tap = UITapGestureRecognizer(target: self, action: #selector(doubleTapped))
tap.numberOfTapsRequired = 2
view.addGestureRecognizer(tap)
记住,UIButton是一个UIView,就像苹果的about
一样。触地重复:

控件中的重复触地事件;对于此事件,UITouch tapCount方法的值大于1

每次用户点击按钮不止一次时,都会触发此事件,因此四次点击将触发事件三次

要仅触发两次点击,您需要创建一个
uitappostation
,并在其
numberOfTapsRequired
上设置2:

let-tappostate=uitappostaturerecognizer(目标:self,动作:#选择器(clickedRepeatOnPlate))
tappostate.numberOfTapsRequired=2
按钮。添加手势识别器(点击手势)
编辑

如果需要将sender按钮作为函数参数,可以执行以下操作:

 func addDoubleTapEvent() {
   let tapGesture = UITapGestureRecognizer(target: self, action: #selector(clickedRepeatOnPlate(gesture:)))
   tapGesture.numberOfTapsRequired = 2
   button.addGestureRecognizer(tapGesture)  
 }

 @objc func clickedRepeatOnPlate(gesture: UITapGestureRecognizer) {
     guard let button = gesture.view as? UIButton else { return }
     print(button.titleLabel?.text)
 }
输出

可选(“按钮”)