以下相关代码。我希望每个图像在整个动画周期中出现时都有淡入效果。运行此代码时,gif动画影响可行,但不透明度更改代码不起作用。任何帮助将不胜感激。谢谢!
import UIKit
class View1: UIViewController {
@IBOutlet weak var imageView: UIImageView!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
var imagesNames = ["ALA0.jpeg", "ALA1.jpeg", "ALA2.png", "ALA3.png", "ALA4.png", "ALA5.png", "ALA6.png", "ALA7.png", "ALA8.png", "ALA9.png", "ALA10.png"]
var images = [UIImage]()
for i in 0..<imagesNames.count{
images.append(UIImage(named: imagesNames[i])!)
imageView.alpha = 0
UIImageView.animate(withDuration: 1.0) {
self.imageView.alpha = 1
}
}
imageView.animationImages = images
imageView.animationDuration = 6.0
self.imageView.startAnimating()
}
}
答案 0 :(得分:0)
尝试以下代码:已更新
for i in 0..<imagesNames.count{
images.append(UIImage(named: imagesNames[i])!)
imageView.alpha = 0
UIImageView.animate(withDuration: 0.9, delay: 0, options: [UIViewAnimationOptions.curveEaseInOut,.repeat], animations: {
self.imageView.alpha = 1
}, completion: nil)
}
imageView.animationImages = images
imageView.animationDuration = 5 // I am using 5 images to perform animation
注意:从上面的代码开始。你会得到一个帧到帧的华丽效果,你必须使用动画持续时间和总动画持续时间来同步效果。
答案 1 :(得分:0)
问题是你的动画在for
循环中。
你有11个图像,所以从imageView.alpha = 0
执行动画的4行正在被执行11次,这会混淆动画系统。
您应该在设置动画图像的位置或持续时间内将alpha设置为0.
至于实际的动画,你应该把它放在viewDidAppear:
中。您不希望在视图位于屏幕之前启动动画,否则它似乎不会从头开始。
最后,实例化图像数组的更好方法是
let images = imageNames.flatMap { UIImage(named: $0) }
即使是Swiftier,也无需声明所有名称(另外,UIImage(named:)
会为您找到图片扩展名,无需添加。)
let images = (0..<11).flatMap { UIImage(named: "ALA\($0)") }