为什么我的UIButton背景图层会生成动画,如何阻止它?

Dou*_*ith 1 cocoa-touch uibutton uiview ios swift

我有一个自定义UIButton子类,当我设置hidden背景层的性质是动画阿尔法从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
}
Run Code Online (Sandbox Code Playgroud)

Abh*_*ert 5

除非禁用动画,否则Core Animation中的所有内容(即"CALayer"中的"CA")都将进行动画处理.

有几种方法可以禁用默认动画,但唯一适用于所有情况的方法是:

CATransaction.begin()
CATransaction.setDisableActions(true)

subLayer.hidden = false

CATransaction.commit()
Run Code Online (Sandbox Code Playgroud)