首页 > 解决方案 > Swift:在动画过程中中途更改 UIImageView 图像

问题描述

我有这个代码来创建动画imageView

        imageView.layer.anchorPoint = CGPoint(x: 0, y: 0.5)

        var transform = CATransform3DIdentity
        transform.m34 = -0.001
        imageView.layer.transform = transform
        UIView.animate(withDuration: 3) {
            self.imageView.layer.transform = CATransform3DRotate(transform, .pi, 0, 1, 0)

            self.imageViewCenterConstraintX.constant = 0

            self.view.layoutIfNeeded()
        }

在此代码imageView中旋转 180 度。我想在imageView旋转 90 度后更改图像。

标签: iosswiftuiimageviewcore-animation

解决方案


You should chain the two animations, each rotating with pi/2.

imageView.layer.anchorPoint = CGPoint(x: 0, y: 0.5)

var transform = CATransform3DIdentity
transform.m34 = -0.001
imageView.layer.transform = transform
UIView.animate(withDuration: 1.0, animations: 
{
   self.imageView.layer.transform = CATransform3DRotate(transform, .pi/2, 0, 1, 0)
   self.imageViewCenterConstraintX.constant = 0  
 }) 
     { 
         (bCompleted) in
         if (bCompleted)
         {
            self.imageView?.layer.transform = CATransform3DRotate(transform, .pi/2, 0, 1, 0)
         }

         UIView.animate(withDuration: 1.0, animations: 
         {
              self.imageView.layer.transform = CATransform3DRotate(transform, .pi*0.999, 0, 1, 0)
              self.imageView.image = UIImage.init(named:"anotherImage")
         }, 
         completion: 
         { 
            (bFinished) in
            //Whatever
         })
  }

推荐阅读