Ios 将UITapGestureRecognitor添加到UIControl
我有一个以编程方式创建的滑块类(Ios 将UITapGestureRecognitor添加到UIControl,ios,swift,uitapgesturerecognizer,uicontrol,Ios,Swift,Uitapgesturerecognizer,Uicontrol,我有一个以编程方式创建的滑块类(类滑块:UIControl),我想添加双击手势以将其调整为默认设置。不幸的是,我不能像以前在SpriteKit中那样实现UITapGestureRecognitor 守则的一部分: class Slider: UIControl{ ... let doubleTap : UITapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(doubleTapped
类滑块:UIControl
),我想添加双击手势以将其调整为默认设置。不幸的是,我不能像以前在SpriteKit中那样实现UITapGestureRecognitor
守则的一部分:
class Slider: UIControl{
...
let doubleTap : UITapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(doubleTapped))
...
init(){
...
doubleTap.numberOfTapsRequired = 1
addGestureRecognizer(doubleTap)
}
func doubleTapped(){
print("double tapped")
}
}
现在我只想实现手势识别器,然后添加我需要做的事情。此外,我还实现了touchesMoved和touchesbeated。好的,答案很简单,不需要代表
class Slider: UIControl{
...
init(){
...
let doubleTap : UITapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(doubleTapped))
doubleTap.numberOfTapsRequired = 1
addGestureRecognizer(doubleTap)
}
func doubleTapped(){
print("double tapped")
}
}
好的,答案很简单,不需要代表
class Slider: UIControl{
...
init(){
...
let doubleTap : UITapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(doubleTapped))
doubleTap.numberOfTapsRequired = 1
addGestureRecognizer(doubleTap)
}
func doubleTapped(){
print("double tapped")
}
}
首先,let中的初始化选择器将不起作用,此时对象尚未完全初始化,请将其移动到init。然后尝试使用委托来确定未调用选择器的原因,可能是某些系统手势识别器从您的系统中获取事件。为什么
init
和双击方法不在类之外,而不是类的一部分?@rmaddy这是一个错误,init和doubleTapped在类中,请编辑您的问题,显示正确的代码。@vojer如何在这里使用委托doubleTap.delegate=self
返回错误:无法将“Slider”类型的值分配给“UIgestureRecognitizerDelegate”类型首先,let中的初始化选择器不起作用,对象此时未完全初始化,请将其移动到init。然后尝试使用委托来确定未调用选择器的原因,可能是某些系统手势识别器从您的系统中获取事件。为什么init
和双击方法不在类之外,而不是类的一部分?@rmaddy这是一个错误,init和doubleTapped在类中,请编辑您的问题,显示正确的代码。@vojer如何在这里使用委托doubleTap.delegate=self
返回错误:无法将“Slider”类型的值分配给“UIgestureRecognitizerDelegate”类型