如何sorting两个animation之间的延迟

我有一个UIImageView ,我想旋转180度,占用1秒,然后我想在这个位置等待1秒,然后旋转180度回到原来的位置占用1秒。

我如何做到这一点? 我已经尝试了100个方法,它不断回弹而不是回转

编辑:我忘了添加我需要这个重复无限期

您可以在UIView.animate中呈现的completionHandler中执行第二个animation

 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 */ }) } 

现在你可能会有另一个问题。

如果使用CGAffineTransform旋转视图,并且为每个animation指定一个新的此types的对象到view.transform,那么将会丢失以前的变换操作

所以,根据这个post: 如何在Swift中应用多个转换 ,你需要连接转换操作

有2个animation块的示例

这是一个旋转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) } 

。重复animation和.autoreverse的例子

.animate方法使您可以设置一些animation选项。 特别是结构UIViewAnimationOptions包含:

  1. .repeat,它无限地重复你的animation块
  2. .autoreverse,它将您的视图恢复到原始状态

考虑到这一点,你可以这样做:

 var transform = view.transform.rotated(by: 180) UIView.animate(withDuration: 2, delay: 0, options: [.repeat, .autoreverse], animations: { self.myView.transform = transform }) 

但是你需要在两个animation之间延迟,所以你需要做这个技巧:

recursionanimation的例子和延迟1秒

只需在ViewController中创build一个方法来为视图添加animation效果。 在上一个completionHandler中,只需调用该方法创build一个无限循环即可。

最后你需要调用viewDidAppear方法来启动animation。

 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() }) } } 

所有你需要做的是创build一个keyFrameanimation 。 它被devise为将多个animation链接在一起,并在第一个关键帧中通过PI旋转你的UIImageView子类,然后在第二个关键帧中将其转换回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 } }) 

结果:

在这里输入图像说明

编辑:这是一个例子如何使它无限期地运行。 我想你的图像是在一个viewController,你持有一些你的imageView引用。

因此,例如,在viewDidAppear ,调用一个触发animation的函数,而不是在animation的完成块中,再次调用相同的函数。

 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() } } }