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

标签 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
}

最佳答案

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

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

CATransaction.begin()
CATransaction.setDisableActions(true)

subLayer.hidden = false

CATransaction.commit()

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

相关文章:

ios - 在模拟器上崩溃,而不是在设备上

ios - 将苹果设备注册为开发者设备会对它做什么?

iphone - 何时捕获 UILabel 的高度?

ios - 触摸开始 : inside UITableView

ios - 在我的 iOS 应用程序中,调用 viewDidLoad 时如何知道设备方向?

ios - 你如何以编程方式从 View Controller 中画一条线?

iOS Objective-C 在自定义框架中显示 PNG 图像

iphone - 如何在 Objective-C 中创建一个 json 字符串?

ios - 如何模块化监听通知的类?

objective-c - @property 和@synthesize 有什么区别?