Swift: 在动画中途更改 UIImageView 图像

Swift: change UIImageView image midway during animation

我有创建动画的代码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 度后更改图像。

使用下面的那个。使用代码更改完成处理程序字段中的图像。

UIView Animation with completion

你可以使用 DispatchQueue.main.asyncAfter.

    UIView.animate(withDuration: 3) {
        DispatchQueue.main.asyncAfter(deadline: .now() + 1.5) {
            // Change your image here
        }
        self.imageView.layer.transform = CATransform3DRotate(transform, .pi, 0, 1, 0)

        self.imageViewCenterConstraintX.constant = 0

        self.view.layoutIfNeeded()
    }

您应该链接这两个动画,每个动画都以 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
         })
  }