gpt4 book ai didi

ios - Swift 中的 UIImageView 动画有时间延迟

转载 作者:行者123 更新时间:2023-11-28 10:25:17 25 4
gpt4 key购买 nike

我遇到了这个问题,我希望 UIImage 在两秒内发光,然后恢复到正常状态。

鉴于此问题,我目前拥有的是:

我有引用的 ImageView ,从 0 到 9。

@IBOutlet weak var imageOne: UIImageView!
@IBOutlet weak var imageTwo: UIImageView!

等等

然后我将它们添加到 viewDiDLoad() 函数中的 subview :

override func viewDidLoad() {
super.viewDidLoad()
self.view.addSubview(imageOne)
self.view.addSubview(imageTwo)
etc.
}

这是我对着色函数的实现:

func colorize(imageView: UIImageView, color: CGColorRef) {
imageView.layer.shadowColor = color
imageView.layer.shadowRadius = 7.0
imageView.layer.shadowOpacity = 0.9
imageView.layer.shadowOffset = CGSizeZero
imageView.layer.masksToBounds = false
}

现在,我正在尝试为通过此调用的两个 ImageView 制作动画。 ab 只是从前一个 View 中获取的随机数。在此示例中,它们将是 1 和 3。:

UIView.animateWithDuration(2.0, delay: 0, options: nil, animations: { () -> Void in
self.colorize(labelArray[a.toInt()!], color:self.green)
}, completion: nil)

UIView.animateWithDuration(2.0, delay: 2.0, options: nil, animations: { () -> Void in
self.colorize(labelArray[b.toInt()!], color:self.cyan)
}, completion: nil)

现在,这是 View 之前的样子 -

enter image description here

这是之后的样子,但是两个动画同时发生。没有过渡。它只是自动应用发光 -

enter image description here

感谢您的帮助!

最佳答案

由于您正在尝试为您的 UIImageView 层制作动画,请尝试使用 Core Animation 而不是 UIView 动画 block ,因为必须使用 Core Animation 来制作阴影层的动画看法。如果您只是想使阴影淡入,请尝试使用此设置阴影不透明度的动画:

override func performGlowAnimations {

self.colorize(labelArray[a.toInt()!], color:self.green)

// Delay the second call by 2 seconds
let delay = 2.0 * Double(NSEC_PER_SEC)
var time = dispatch_time(DISPATCH_TIME_NOW, Int64(delay))
dispatch_after(time, dispatch_get_main_queue(), {
self.colorize(labelArray[b.toInt()!], color:self.cyan)
})
}

func colorize(imageView: UIImageView, color: CGColorRef) {

// Set the image's shadowColor, radius, offset, and
// set masks to bounds to false
imageView.layer.shadowColor = color
imageView.layer.shadowRadius = 7.0
imageView.layer.shadowOffset = CGSizeZero
imageView.layer.masksToBounds = false

// Animate the shadow opacity to "fade it in"
let shadowAnim = CABasicAnimation()
shadowAnim.keyPath = "shadowOpacity"
shadowAnim.fromValue = NSNumber(float: 0.0)
shadowAnim.toValue = NSNumber(float: 0.9)
shadowAnim.duration = 2.0

imageView.layer.addAnimation(shadowAnim, forKey: "shadowOpacity")
imageView.layer.shadowOpacity = 0.9
}

关于ios - Swift 中的 UIImageView 动画有时间延迟,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27610118/

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