对不起,这可能看起来像一个愚蠢的问题。如果我有一个从UIButton继承的类LargeButton,有没有办法让我说当单击任何LargeButton时,所有LargeButton对象都有动画并消失?你可以让一个类的所有对象用一个方法调用做同样的事情吗?
UIView.animateWithDuration(0.4, delay: 0, options: UIViewAnimationOptions.CurveEaseOut, animations: {
AllLargeButtons.alpha = 0
}, completion: { finished in
AllLargeButtons.hidden = true
有没有办法做到这样的事情,而不是先把所有的LargeButton存储在数组中?
编辑:
class LargeButton: UIButton {
required init?(coder: NSCoder) {
super.init(coder: coder)
NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(oneLargeButtonTapped), name: "oneOfLargeButtonsTapped", object: nil)
}
func oneLargeButtonTapped() {
UIView.animateWithDuration(0.4, delay: 0, options: UIViewAnimationOptions.CurveEaseOut, animations: {
self.alpha = 0 // Here you will get the animation you want
}, completion: { finished in
self.hidden = true // Here you hide it when animation done
})
}
deinit {
NSNotificationCenter.defaultCenter().removeObserver(self)
}
}
class viewController: UIViewController {
@IBAction func buttonTapped(sender: UIButton) {
NSNotificationCenter.defaultCenter().postNotificationName("oneOfLargeButtonsTapped", object: nil, userInfo: nil)
}
}
创建LargeButton:
lazy var aButton: LargeButton = {
let button = LargeButton()
NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(oneLargeButtonTapped), name: "oneOfLargeButtonsTapped", object: nil)
// button.addTarget(self, action: #selector(oneLargeButtonTapped), forControlEvents: .TouchUpInside)
return button
}()
或者引发自定义事件,然后从每个按钮实例处理它。 –
你是什么意思,@ chrisp_68? – Alexander
@AlexanderMomchliov如果我以编程方式编写它,我没有真正的选择,而是要为每个现有的LargeButton编写动作,我猜对了? – tryingtolearn