Swift:在动画过程中更改UIImageView图像

时间:2018-07-01 10:32:18

标签: ios swift uiimageview core-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度后更改图像。

3 个答案:

答案 0 :(得分:1)

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

答案 1 :(得分:0)

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

UIView Animation with completion

答案 2 :(得分:0)

您可以使用/usr/bin/Xvfb :1 -screen 0 640x480x24 -ac +extension GLX +render -noreset

DispatchQueue.main.asyncAfter