Swift 3 - 动画
Swift 3 - Animation
我正在我的 iPad 应用程序的底部制作一张图片的动画。目前 imageView 使用以下代码片段进行动画处理:
UIView.animate(withDuration: 5, delay: 0,
options: [.repeat, .curveEaseIn, .curveEaseOut],
animations: {
self.imageView.center.x += self.view.bounds.width
},
completion: nil
)
我有一个整数 intVal = 0;
我需要根据 intVal 的值更改图像视图的图像。如何比较 intVal 并在动画开始前更改图像?
理想情况下,最终代码片段应如下所示:
UIView.animate(withDuration: 5, delay: 0,
options: [.repeat, .curveEaseIn, .curveEaseOut],
animations: {
self.imageView.center.x += self.view.bounds.width
},
completion:
if(intVal == 1){
imageView.image = differentImg.png;
}
...
else if(intVal == 10){
imageView.image = AnotherDifferentImg.png;
}
)
编辑:
该方法在ViewDidAppear中被调用一次。动画每 5 秒(如持续时间所示)重复一次(如 .repeat 选项所示)。图片必须判断每次动画重复时是否需要改变。
您需要使用计时器来实现您的目标,因为您不仅需要动画,还需要一些其他逻辑。所以请定时器处理你的重复,然后在里面做其他工作
var timer = Timer.scheduledTimer(withTimeInterval: 6, repeats: true) {
(_) in
//Set image first base on intVal
if inVal == 1 {
self.imageView.image = UIImage(named: "a.png")
}else{
self.imageView.image = UIImage(named: "b.png")
}
//Then start animation
UIView.animate(withDuration: 5, delay: 0, options: [.curveEaseIn, .curveEaseOut], animations: {
self.imageView.center.x += self.view.bounds.width
}, completion: nil)
}
尝试创建一个方法,该方法将首先检查值,然后在不重复选项的情况下为视图设置动画,但在完成块中调用自身。
func animateImageView {
if(intVal == 1){
imageView.image = differentImg.png;
}
...
else if(intVal == 10){
imageView.image = AnotherDifferentImg.png;
}
UIView.animate(withDuration: 5, delay: 0,
options: [.curveEaseIn, .curveEaseOut],
animations: {
self.imageView.center.x += self.view.bounds.width
},
completion: animateImageView())
并在viewDidAppear中调用此方法
我正在我的 iPad 应用程序的底部制作一张图片的动画。目前 imageView 使用以下代码片段进行动画处理:
UIView.animate(withDuration: 5, delay: 0,
options: [.repeat, .curveEaseIn, .curveEaseOut],
animations: {
self.imageView.center.x += self.view.bounds.width
},
completion: nil
)
我有一个整数 intVal = 0;
我需要根据 intVal 的值更改图像视图的图像。如何比较 intVal 并在动画开始前更改图像?
理想情况下,最终代码片段应如下所示:
UIView.animate(withDuration: 5, delay: 0,
options: [.repeat, .curveEaseIn, .curveEaseOut],
animations: {
self.imageView.center.x += self.view.bounds.width
},
completion:
if(intVal == 1){
imageView.image = differentImg.png;
}
...
else if(intVal == 10){
imageView.image = AnotherDifferentImg.png;
}
)
编辑:
该方法在ViewDidAppear中被调用一次。动画每 5 秒(如持续时间所示)重复一次(如 .repeat 选项所示)。图片必须判断每次动画重复时是否需要改变。
您需要使用计时器来实现您的目标,因为您不仅需要动画,还需要一些其他逻辑。所以请定时器处理你的重复,然后在里面做其他工作
var timer = Timer.scheduledTimer(withTimeInterval: 6, repeats: true) {
(_) in
//Set image first base on intVal
if inVal == 1 {
self.imageView.image = UIImage(named: "a.png")
}else{
self.imageView.image = UIImage(named: "b.png")
}
//Then start animation
UIView.animate(withDuration: 5, delay: 0, options: [.curveEaseIn, .curveEaseOut], animations: {
self.imageView.center.x += self.view.bounds.width
}, completion: nil)
}
尝试创建一个方法,该方法将首先检查值,然后在不重复选项的情况下为视图设置动画,但在完成块中调用自身。
func animateImageView {
if(intVal == 1){
imageView.image = differentImg.png;
}
...
else if(intVal == 10){
imageView.image = AnotherDifferentImg.png;
}
UIView.animate(withDuration: 5, delay: 0,
options: [.curveEaseIn, .curveEaseOut],
animations: {
self.imageView.center.x += self.view.bounds.width
},
completion: animateImageView())
并在viewDidAppear中调用此方法