mik*_*lam 5 core-animation ios swift
我有一个UIImageView我想旋转180度,占用1秒,然后我想在此位置等待1秒,然后将180度旋转回到原始位置,占用1秒。
我该如何完成?我尝试了100种方法,但它会不断回弹而不是回滚
编辑:我忘了补充,我需要这个无限期地重复
您可以在UIView.animate上显示的完成处理程序中执行第二个动画
let duration = self.transitionDuration(using: transitionContext)
let firstAnimDuration = 0.5
UIView.animate(withDuration: firstAnimDuration, animations: {
/* Do here the first animation */
}) { (completed) in
let secondAnimDuration = 0.5
UIView.animate(withDuration: secondAnimDuration, animations: {
/* Do here the second animation */
})
}
Run Code Online (Sandbox Code Playgroud)
现在您可能会遇到另一个问题。
如果使用CGAffineTransform旋转视图,并且为每个动画将一个这种类型的新对象分配给view.transform,则将丢失先前的变换操作
因此,根据这篇文章:如何在Swift中应用多个转换,您需要进行转换操作
这是旋转180度并在1秒后返回原点的示例:
let view = UIView.init(frame: CGRect.init(origin: self.view.center, size: CGSize.init(width: 100, height: 100)))
view.backgroundColor = UIColor.red
self.view.addSubview(view)
var transform = view.transform
transform = transform.rotated(by: 180)
UIView.animate(withDuration: 2, animations: {
view.transform = transform
}) { (completed) in
transform = CGAffineTransform.identity
UIView.animate(withDuration: 2, delay: 1, options: [], animations: {
view.transform = transform
}, completion: nil)
}
Run Code Online (Sandbox Code Playgroud)
.animate方法使您能够设置一些动画选项。特别是结构UIViewAnimationOptions包含:
考虑到这一点,您可以执行以下操作:
var transform = view.transform.rotated(by: 180)
UIView.animate(withDuration: 2, delay: 0, options: [.repeat, .autoreverse], animations: {
self.myView.transform = transform
})
Run Code Online (Sandbox Code Playgroud)
但是您需要在两个动画之间进行延迟,因此您需要执行以下操作:
只需在ViewController中创建一个动画视图的方法即可。在最后一个完成处理程序中,只需调用该方法即可创建无限循环。
最后,您需要在viewDidAppear上调用方法以启动动画。
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
self.animation()
}
func animation() {
var transform = view.transform
transform = transform.rotated(by: 180)
UIView.animate(withDuration: 2, delay: 0, options: [], animations: {
self.myView.transform = transform
}) { bool in
transform = CGAffineTransform.identity
UIView.animate(withDuration: 2, delay: 1, options: [], animations: {
self.myView.transform = transform
}, completion: { bool in
self.animation()
})
}
}
Run Code Online (Sandbox Code Playgroud)
您需要做的就是创建一个keyFrame动画。它旨在将多个动画链接在一起,并在第一个关键帧中旋转UIImageView子类PI,然后在第二个关键帧中将其转换回identity。
let rotateForwardAnimationDuration: TimeInterval = 1
let rotateBackAnimationDuration: TimeInterval = 1
let animationDuration: TimeInterval = rotateForwardAnimationDuration + rotateBackAnimationDuration
UIView.animateKeyframes(withDuration: animationDuration, delay: 0, options: [], animations: {
UIView.addKeyframe(withRelativeStartTime: 0, relativeDuration: rotateForwardAnimationDuration) {
self.imageView.transform = CGAffineTransform(rotationAngle: .pi)
}
UIView.addKeyframe(withRelativeStartTime: 0.5, relativeDuration: rotateBackAnimationDuration) {
self.imageView.transform = .identity
}
})
Run Code Online (Sandbox Code Playgroud)
结果:
编辑: 这是一个示例,如何使其无限期运行。我想您的图像在viewController中,并且您拥有对imageView的一些引用。
因此,例如,在上viewDidAppear,调用触发动画的函数,然后在动画的完成块中再次调用同一函数。
class ViewController: UIViewController {
@IBOutlet weak var imageView: UIImageView!
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
self.runRotateAnimation()
}
func runRotateAnimation() {
let rotateForwardAnimationDuration: TimeInterval = 1
let rotateBackAnimationDuration: TimeInterval = 1
let animationDuration: TimeInterval = rotateForwardAnimationDuration + rotateBackAnimationDuration
UIView.animateKeyframes(withDuration: animationDuration, delay: 0, options: [], animations: {
UIView.addKeyframe(withRelativeStartTime: 0, relativeDuration: rotateForwardAnimationDuration) {
self.imageView.transform = CGAffineTransform(rotationAngle: .pi)
}
UIView.addKeyframe(withRelativeStartTime: 0.5, relativeDuration: rotateBackAnimationDuration) {
self.imageView.transform = .identity
}
}) { (isFinished) in
// To loop it continuosly, just call the same function from the completion block of the keyframe animation
self.runRotateAnimation()
}
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
4319 次 |
| 最近记录: |