UIButton子类@IBAction不工作
我创build了一个UIButton的子类,所以我可以得到一个水龙头的animation效果,效果起作用,什么不起作用,当我试着把它连接到我的视图控制器@IBAction。 该函数没有被调用。 我有我的记分板中的button类的子类设置。 这是我的代码:
import UIKit class SpringyButton: UIButton { /* // Only override drawRect: if you perform custom drawing. // An empty implementation adversely affects performance during animation. override func drawRect(rect: CGRect) { // Drawing code } */ override func touchesBegan(touches: NSSet, withEvent event: UIEvent) { UIView.animateWithDuration(0.4, delay: 0.0, usingSpringWithDamping: 0.9, initialSpringVelocity: 0.9, options: UIViewAnimationOptions.AllowUserInteraction, animations: { self.layer.transform = CATransform3DMakeScale(0.8, 0.8, 1) }, completion: nil) } override func touchesEnded(touches: NSSet, withEvent event: UIEvent) { UIView.animateWithDuration(0.4, delay: 0.0, usingSpringWithDamping: 0.9, initialSpringVelocity: 0.9, options: UIViewAnimationOptions.AllowUserInteraction, animations: { self.layer.transform = CATransform3DMakeScale(1, 1, 1) }, completion: nil) } override func touchesCancelled(touches: NSSet!, withEvent event: UIEvent!) { UIView.animateWithDuration(0.4, delay: 0.0, usingSpringWithDamping: 0.9, initialSpringVelocity: 0.9, options: UIViewAnimationOptions.AllowUserInteraction, animations: { self.layer.transform = CATransform3DMakeScale(1, 1, 1) }, completion: nil) } override func touchesMoved(touches: NSSet, withEvent event: UIEvent) { UIView.animateWithDuration(0.4, delay: 0.0, usingSpringWithDamping: 0.9, initialSpringVelocity: 0.9, options: UIViewAnimationOptions.AllowUserInteraction, animations: { self.layer.transform = CATransform3DMakeScale(1, 1, 1) }, completion: nil) } }
和我的ViewController中的IBAction:
@IBAction func test(sender: AnyObject) { println("go") }
我不确定是什么原因造成的,有什么想法?
边注意:我读过,UIButton的子类化并不是一个好主意,将UIView子类化,并将其与一个轻敲手势识别器连接起来会更好吗?
谢谢。
问题是你只是重写触摸事件。 重写函数后,必须调用super。 例如在下面的touchesBegan
函数中:
override func touchesBegan(touches: NSSet, withEvent event: UIEvent) { UIView.animateWithDuration(0.4, delay: 0.0, usingSpringWithDamping: 0.9, initialSpringVelocity: 0.9, options: UIViewAnimationOptions.AllowUserInteraction, animations: { self.layer.transform = CATransform3DMakeScale(0.8, 0.8, 1) }, completion: nil) super.touchesBegan(touches, withEvent: event) }
像这样,你必须添加超级所有触摸事件,你重写。 如果您遇到任何问题,请告诉我。
我相信Interface Builder只使用UIButton。 所以子类化不会真的起作用。 如果你真的想使用你的自定义类,你可以以编程方式创build它。
let myButton : SpringyButton = SpringyButton()
让我知道,如果这个工作和/或如果这就是你正在寻找!