gpt4 book ai didi

ios - 为什么我的 UIButton 的背景层会出现动画,我该如何停止它?

转载 作者:搜寻专家 更新时间:2023-11-01 06:10:22 25 4
gpt4 key购买 nike

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

最佳答案

在 Core Animation(即“CALayer”中的“CA”)中所做的一切都将被动画化,除非您禁用动画。

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

CATransaction.begin()
CATransaction.setDisableActions(true)

subLayer.hidden = false

CATransaction.commit()

关于ios - 为什么我的 UIButton 的背景层会出现动画,我该如何停止它?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26351759/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com