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

时间:2014-10-14 02:31:12

标签: ios cocoa-touch uiview swift uibutton

我有一个自定义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
}

2 个答案:

答案 0 :(得分:5)

除非您禁用动画,否则Core Animation中的所有内容(" CA" in" CALayer")都将被设置为动画。

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

CATransaction.begin()
CATransaction.setDisableActions(true)

subLayer.hidden = false

CATransaction.commit()

答案 1 :(得分:0)

禁用图层与CATransaction.setDisableActions(true)

一起使用的自动动画