【问题标题】:Getting the color of a UIView during an animation in Swift?在 Swift 中的动画期间获取 UIView 的颜色?
【发布时间】:2016-04-08 17:36:05
【问题描述】:

我正在使用动画在两种颜色之间淡化按钮,并且需要在按下按钮时获取其当前颜色。

这是动画代码:

UIView.animateWithDuration(speed, delay: 0.0, options:[UIViewAnimationOptions.Repeat, UIViewAnimationOptions.Autoreverse, UIViewAnimationOptions.AllowUserInteraction, UIViewAnimationOptions.CurveEaseOut], animations: {
            targetView.layer.backgroundColor = self.randomColors(40).CGColor
            targetView.layer.backgroundColor = self.randomColors(250).CGColor

            }, completion: nil)

使用targetView.layer.backgroundColor 读取颜色每次都会给我相同的结果,而不是特定时刻的颜色。我尝试了一些函数来获取屏幕上特定像素的颜色也无济于事,它们每次都返回相同的颜色。

按下按钮后动画不需要继续,我只需要获取按下按钮时的颜色即可。

任何帮助都会很棒,谢谢!

【问题讨论】:

    标签: ios swift animation colors


    【解决方案1】:

    您可以使用presentationLayer 获得给定图层的屏幕状态近似值,包括任何当前正在运行的动画。它返回一个可选的AnyObject——所以你必须确保你正确解开它。

    例如:

    if let presentationLayer = targetView.layer.presentationLayer() as? CALayer {   
        let currentBackgroundColor = presentationLayer.backgroundColor
    }
    

    请注意,从这里返回的层是一个副本——因此对它的任何修改都不会反映在其他任何地方。

    【讨论】:

    • 谢谢!这正是我所追求的。非常感谢。
    • @Stephen 乐于助人:)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-07-29
    • 1970-01-01
    • 1970-01-01
    • 2010-10-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多