'CustomButton' 类型的值没有成员 'touchDown'
Value of type 'CustomButton' has no member 'touchDown'
我创建了自定义按钮 class 并覆盖了所有触摸方法。它在 swift 2
和 Xcode 7.3.1
中运行良好。但是当我在 Xcode 8.0
中打开相同的应用程序时,它会显示错误:
Value of type 'CustomButton' has no member 'touchDown'
Value of type 'CustomButton' has no member 'touchUpInside'
Value of type 'CustomButton' has no member 'touchDragExit'
Value of type 'CustomButton' has no member 'touchDragEnter'
Value of type 'CustomButton' has no member 'touchCancel'
这是我的代码:
import UIKit
@IBDesignable
@objc public class CustomButton: UIButton {
private func addTargets() {
//------ add target -------
self.addTarget(self, action: #selector(self.touchDown(_:)), for: UIControlEvents.TouchDown)
self.addTarget(self, action: #selector(self.touchUpInside(_:)), for: UIControlEvents.TouchUpInside)
self.addTarget(self, action: #selector(self.touchDragExit(_:)), for: UIControlEvents.TouchDragExit)
self.addTarget(self, action: #selector(self.touchDragEnter(_:)), for: UIControlEvents.TouchDragEnter)
self.addTarget(self, action: #selector(self.touchCancel(_:)), for: UIControlEvents.TouchCancel)
}
func touchDown(sender: CustomButton) {
self.layer.opacity = 0.4
}
func touchUpInside(sender: CustomButton) {
self.layer.opacity = 1.0
}
func touchDragExit(sender: CustomButton) {
self.layer.opacity = 1.0
}
func touchDragEnter(sender: CustomButton) {
self.layer.opacity = 0.4
}
func touchCancel(sender: CustomButton) {
self.layer.opacity = 1.0
}
}
如果谁有解决办法,请告诉我。
如果您想在代码中保留方法 headers,则需要将选择器引用更改为 #selector(touchDown(sender:))
,依此类推。
(一般不需要加前缀self.
。)
请记住,所有函数和方法的第一个参数现在都有一致的标签处理方式。 SE-0046
(用"swift3 selector"搜索可能会发现很多好文章。)
如果你想保留选择器引用,你需要改变如下方法:
func touchDown(_ sender: CustomButton) {
此外,如果您的 class 只有一种 touchDown(...)
方法,#selector(touchDown)
也可以。
我已经按照@OOPer 的建议找到了解决方案,但在小的情况下还需要更改 UIControlEvents
。在 Xcode 7.3.1
中是 UIControlEvents.TouchDown
,但现在必须是 UIControlEvents.touchDown
。
它将像:
self.addTarget(self, action: #selector(touchDown(sender:)), for: UIControlEvents.touchDown)
self.addTarget(self, action: #selector(touchUpInside(sender:)), for: UIControlEvents.touchUpInside)
self.addTarget(self, action: #selector(touchDragExit(sender:)), for: UIControlEvents.touchDragExit)
self.addTarget(self, action: #selector(touchDragEnter(sender:)), for: UIControlEvents.touchDragEnter)
self.addTarget(self, action: #selector(touchCancel(sender:)), for: UIControlEvents.touchCancel)
我创建了自定义按钮 class 并覆盖了所有触摸方法。它在 swift 2
和 Xcode 7.3.1
中运行良好。但是当我在 Xcode 8.0
中打开相同的应用程序时,它会显示错误:
Value of type 'CustomButton' has no member 'touchDown'
Value of type 'CustomButton' has no member 'touchUpInside'
Value of type 'CustomButton' has no member 'touchDragExit'
Value of type 'CustomButton' has no member 'touchDragEnter'
Value of type 'CustomButton' has no member 'touchCancel'
这是我的代码:
import UIKit
@IBDesignable
@objc public class CustomButton: UIButton {
private func addTargets() {
//------ add target -------
self.addTarget(self, action: #selector(self.touchDown(_:)), for: UIControlEvents.TouchDown)
self.addTarget(self, action: #selector(self.touchUpInside(_:)), for: UIControlEvents.TouchUpInside)
self.addTarget(self, action: #selector(self.touchDragExit(_:)), for: UIControlEvents.TouchDragExit)
self.addTarget(self, action: #selector(self.touchDragEnter(_:)), for: UIControlEvents.TouchDragEnter)
self.addTarget(self, action: #selector(self.touchCancel(_:)), for: UIControlEvents.TouchCancel)
}
func touchDown(sender: CustomButton) {
self.layer.opacity = 0.4
}
func touchUpInside(sender: CustomButton) {
self.layer.opacity = 1.0
}
func touchDragExit(sender: CustomButton) {
self.layer.opacity = 1.0
}
func touchDragEnter(sender: CustomButton) {
self.layer.opacity = 0.4
}
func touchCancel(sender: CustomButton) {
self.layer.opacity = 1.0
}
}
如果谁有解决办法,请告诉我。
如果您想在代码中保留方法 headers,则需要将选择器引用更改为 #selector(touchDown(sender:))
,依此类推。
(一般不需要加前缀self.
。)
请记住,所有函数和方法的第一个参数现在都有一致的标签处理方式。 SE-0046
(用"swift3 selector"搜索可能会发现很多好文章。)
如果你想保留选择器引用,你需要改变如下方法:
func touchDown(_ sender: CustomButton) {
此外,如果您的 class 只有一种 touchDown(...)
方法,#selector(touchDown)
也可以。
我已经按照@OOPer 的建议找到了解决方案,但在小的情况下还需要更改 UIControlEvents
。在 Xcode 7.3.1
中是 UIControlEvents.TouchDown
,但现在必须是 UIControlEvents.touchDown
。
它将像:
self.addTarget(self, action: #selector(touchDown(sender:)), for: UIControlEvents.touchDown)
self.addTarget(self, action: #selector(touchUpInside(sender:)), for: UIControlEvents.touchUpInside)
self.addTarget(self, action: #selector(touchDragExit(sender:)), for: UIControlEvents.touchDragExit)
self.addTarget(self, action: #selector(touchDragEnter(sender:)), for: UIControlEvents.touchDragEnter)
self.addTarget(self, action: #selector(touchCancel(sender:)), for: UIControlEvents.touchCancel)