gpt4 book ai didi

ios - UIButton 子类 @IBAction 不工作

转载 作者:可可西里 更新时间:2023-11-01 02:27:05 25 4
gpt4 key购买 nike

我为 UIButton 创建了一个子类,这样我就可以在点击时获得动画效果,效果有效,但不起作用的是当我尝试将它连接到我的 View Controller 中的 @IBAction 时。该函数未被调用。我将子类设置为记分牌中的按钮类。这是我的代码:

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)
}

像这样,您必须将 super 添加到您覆盖的所有触摸事件中。如果您遇到任何问题,请告诉我。

关于ios - UIButton 子类 @IBAction 不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27151246/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com