【发布时间】:2020-03-30 21:40:10
【问题描述】:
我正在尝试创建一个滚动浏览一系列图像的动画。以下代码没有任何动画,只有该系列的最后一张图片出现在屏幕上。如何修复此动画?
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(true)
imageSelection(score: 50)
}
func imageSelection(score: Int) {
var myScore = score
if score < 10 {
myScore = 1
}
else if score < 20 && score > 10 {
myScore = 2
}
else if score < 30 && score > 20 {
myScore = 3
}
else if score < 40 && score > 30 {
myScore = 4
}
else if score < 50 && score > 40 {
myScore = 5
}
else if score == 50 {
myScore = 6
}
for i in 1...myScore{
UIView.animate(withDuration: 0.5) {
self.cookieImage.image = UIImage(named: "rewardsCookie\(i)")
self.view.layoutIfNeeded()
}
}
}
【问题讨论】:
-
您没有为任何可动画属性设置动画,而是同时运行所有
myScore非动画。 -
另外,使用
UIView动画块是无法实现的。您必须使用CAAnimation编写动画。请记住,layoutIfNeeded()仅在约束发生变化时才进行动画处理,而您在此 sn-p 中没有动画(如果您正确设置约束,最多将动画图像大小更改)。如果您想要从一个图像到另一个图像的过渡动画(例如淡入淡出),您必须实现它们。 -
@matt 什么是动画属性?以及如何分别运行它们?
-
@VladRusu 你能回答我如何为此创建一个 CAAnimaiton 吗?
-
动画视图属性在 UIView 文档中列出;没有任何关于 UIImageView 仅设置图像会导致任何类型的动画,您必须使用 CABasicAnimation 或类似的“从头开始”编写该动画
标签: ios swift xcode animation viewdidappear