I have created custom button class and override all touches methods. It works fine in swift 2
and Xcode 7.3.1
. But when I open same app in Xcode 8.0
, it will show errors :
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'
Here is my code :
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
}
}
If anyone have any solution, please let me know.
If you want to keep your method headers as are in your code, you need to change the selector references to
#selector(touchDown(sender:))
, and so on.(Generally, you have no need to prefix
self.
.)Remember all functions and methods now have consistent label treatment for their first parameters. SE-0046
(You may find many good articles, searching with "swift3 selector".)
If you want to keep the selector references, you need to change the methods like:
For addition,
#selector(touchDown)
would work, if your class has only onetouchDown(...)
method.I have found solution as @OOPer suggested, but there also need to change
UIControlEvents
in small case. InXcode 7.3.1
it wasUIControlEvents.TouchDown
but now it has to beUIControlEvents.touchDown
.It will be like :