Я создал подкласс для UIButton, чтобы я мог получить эффект анимации при нажатии, эффект работает, что не работает, когда я пытаюсь подключить его к @IBAction в моем просмотрите контроллер. Функция не вызвана. У меня есть подкласс, который задан как класс кнопок на моем табло. Вот мой код:UIButton Subclass @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)
}
}
и мой IBAction в моем ViewController:
@IBAction func test(sender: AnyObject) {
println("go")
}
Я не уверен, что причиной этого, любые идеи?
Боковое примечание: я читал, что подклассификация UIButton - не такая отличная идея, было бы лучше подклассифицировать UIView и подключить это с помощью распознавателя жестов?
Спасибо.
Я бы не подклассифицировал UIButton для этого. Я бы просто добавил его как часть действия или что-то в этом роде. – Fogmeister
@Fogmeister попробовал это, анимация не работает должным образом как действие – mlevi
Вы можете абсолютно сделать это как нормальное действие: D Вам просто нужно добавить действия для 'touchUpInside',' touchDownInside' и т. Д. BTW, I 'd преобразуйте сама кнопку, а не слой кнопки. – Fogmeister