gpt4 book ai didi

ios - 在 Swift 动画期间获取 UIView 的颜色?

转载 作者:行者123 更新时间:2023-11-29 01:05:06 25 4
gpt4 key购买 nike

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

这是动画代码:

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读取颜色每次都会给出相同的结果,而不是特定时刻的颜色。我尝试了一些函数来获取屏幕上特定像素的颜色,但都无济于事,它们每次都返回相同的颜色。

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

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

最佳答案

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

例如:

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

请注意,由此返回的层是一个副本 - 因此对其进行的任何修改都不会反射(reflect)到其他任何地方。

关于ios - 在 Swift 动画期间获取 UIView 的颜色?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36505943/

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