带有完成处理程序的 UIImageView
UIImageView with completion handler
我正在创建一个自定义 activity 指示器视图,但我在为我的指示器实现我想要的动画时遇到了困难。 first part of the animation has the dots emerge. When the dots have emerged the following set of images are looped over to indicate that something is loading. Finally once a condition is met it will trigger the image view to animate the last set of images 使点消失。总共有150张图片。
let iv = UIImageView()
var isAnimating: Bool = false
func startAnimating() {
isAnimating = true
iv.animationImages = (0...40).map { UIImage(named: "loader_\([=10=])")! }
iv.animationDuration = 40 / 30
iv.animationRepeatCount = 1
iv.image = UIImage(named: "loader_40")
iv.startAnimating()
DispatchQueue.main.asyncAfter(deadline: .now() + 40 / 30) {
self.iv.image = UIImage.animatedImage(with: (41...110).map { UIImage(named: "loader_\([=10=])")! }, duration: 70 / 30)
Timer.scheduledTimer(withTimeInterval: 70 / 30, repeats: true) { timer in
if !self.isAnimating {
self.iv.animationImages = (111...150).map { UIImage(named: "loader_\([=10=])")! }
self.iv.image = UIImage(named: "loader_150")
self.iv.startAnimating()
timer.invalidate()
}
}
}
}
前 40 张图像已加载到开始动画中。使用 asyncAfter 我将动画图像设置为包含 70 个与加载动画相关的图像的数组,然后每次加载动画完成时调用计时器以检查是否已将 isAnimating 布尔值设置为 false,然后将图像视图动画图像设置为点消失的最后40张图片
代码有效,但是使用 asyncAfter 和 scheduledTimer 为图像视图设置新的动画图像并不像理想的解决方案。我如何才能为图像视图创建一个完成处理程序,以便当 animationImages 完成播放时我能够将动画图像设置为新值。
请尝试以下操作:
// Create a Core Animation transaction and set its completion block
CATransaction.begin()
CATransaction.setCompletionBlock {
NSLog("Animation complete")
}
// create your animations as before
iv.animationImages = (0...40).map { UIImage(named: "loader_\([=10=])")! }
iv.animationDuration = 40 / 30
iv.animationRepeatCount = 1
iv.image = UIImage(named: "loader_40")
iv.startAnimating()
// Commit the transaction
CATransaction.commit()
在 Core Animation 事务中包装应该提供一个完成块,当动画完成时将被调用。
我正在创建一个自定义 activity 指示器视图,但我在为我的指示器实现我想要的动画时遇到了困难。 first part of the animation has the dots emerge. When the dots have emerged the following set of images are looped over to indicate that something is loading. Finally once a condition is met it will trigger the image view to animate the last set of images 使点消失。总共有150张图片。
let iv = UIImageView()
var isAnimating: Bool = false
func startAnimating() {
isAnimating = true
iv.animationImages = (0...40).map { UIImage(named: "loader_\([=10=])")! }
iv.animationDuration = 40 / 30
iv.animationRepeatCount = 1
iv.image = UIImage(named: "loader_40")
iv.startAnimating()
DispatchQueue.main.asyncAfter(deadline: .now() + 40 / 30) {
self.iv.image = UIImage.animatedImage(with: (41...110).map { UIImage(named: "loader_\([=10=])")! }, duration: 70 / 30)
Timer.scheduledTimer(withTimeInterval: 70 / 30, repeats: true) { timer in
if !self.isAnimating {
self.iv.animationImages = (111...150).map { UIImage(named: "loader_\([=10=])")! }
self.iv.image = UIImage(named: "loader_150")
self.iv.startAnimating()
timer.invalidate()
}
}
}
}
前 40 张图像已加载到开始动画中。使用 asyncAfter 我将动画图像设置为包含 70 个与加载动画相关的图像的数组,然后每次加载动画完成时调用计时器以检查是否已将 isAnimating 布尔值设置为 false,然后将图像视图动画图像设置为点消失的最后40张图片
代码有效,但是使用 asyncAfter 和 scheduledTimer 为图像视图设置新的动画图像并不像理想的解决方案。我如何才能为图像视图创建一个完成处理程序,以便当 animationImages 完成播放时我能够将动画图像设置为新值。
请尝试以下操作:
// Create a Core Animation transaction and set its completion block
CATransaction.begin()
CATransaction.setCompletionBlock {
NSLog("Animation complete")
}
// create your animations as before
iv.animationImages = (0...40).map { UIImage(named: "loader_\([=10=])")! }
iv.animationDuration = 40 / 30
iv.animationRepeatCount = 1
iv.image = UIImage(named: "loader_40")
iv.startAnimating()
// Commit the transaction
CATransaction.commit()
在 Core Animation 事务中包装应该提供一个完成块,当动画完成时将被调用。