簡體   English   中英

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度后更改圖像。

您應該鏈接兩個動畫,每個動畫都以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
         })
  }

使用下面的一種。 使用代碼在完成處理程序字段中更改圖像。

UIView動畫完成

您可以使用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()
    }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM