【发布时间】:2016-08-18 12:43:18
【问题描述】:
抱歉,这似乎是一个愚蠢的问题。如果我有一个继承自 UIButton 的 LargeButton 类,我有办法说当单击任何 LargeButton 时,所有 LargeButton 对象都会动画并消失?
UIView.animateWithDuration(0.4, delay: 0, options: UIViewAnimationOptions.CurveEaseOut, animations: {
AllLargeButtons.alpha = 0
}, completion: { finished in
AllLargeButtons.hidden = true
有没有办法在不先将所有 LargeButtons 存储在数组中的情况下做这样的事情?
编辑:
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)
}
}
创建大按钮:
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
}()
【问题讨论】:
标签: swift animation inheritance uibutton jquery-animate