我有一个自定义UIButton
子类,当我设置背景图层的hidden
属性时,它会将alpha设置为0到1。我希望从0到1的瞬间跳转。为什么它动画制作,如何阻止它这样做?
这是我的代码:
private var subLayer: CALayer!
override func awakeFromNib() {
super.awakeFromNib()
addTarget(self, action: "touchedDown", forControlEvents: .TouchDown)
addTarget(self, action: "canceledTouch", forControlEvents: .TouchUpInside | .TouchUpOutside | .TouchDragExit | .TouchCancel)
}
override func updateConstraints() {
super.updateConstraints()
// Add a background layer that is tight to the text in the button to indicate the button being pressed
subLayer = CALayer()
subLayer.frame = CGRectInset(bounds, -4.0, 5.0)
subLayer.backgroundColor = UIColor(red: 204/255.0, green: 232/255.0, blue: 253/255.0, alpha: 1.0).CGColor
subLayer.cornerRadius = 2.0
subLayer.hidden = true
layer.insertSublayer(subLayer, atIndex: 0)
}
func touchedDown() {
subLayer.hidden = false
}
func canceledTouch() {
subLayer.hidden = true
}
答案 0 :(得分:5)
除非您禁用动画,否则Core Animation中的所有内容(" CA" in" CALayer")都将被设置为动画。
有几种方法可以禁用默认动画,但唯一适用于所有情况的方法是:
CATransaction.begin()
CATransaction.setDisableActions(true)
subLayer.hidden = false
CATransaction.commit()
答案 1 :(得分:0)
禁用图层与CATransaction.setDisableActions(true)