在 swift 中制作包含 3 个动作的动画

do a animation with 3 actions in swift

我希望我的 swift 代码使用单独的不同动作制作动画。这是按顺序排列的。前 2 个动画有效,但是当我添加第 3 个动画时,它不再有效并导致编译错误。您可以在评论所在的位置看到第三个动画。我只想让所有 3 个动画正常工作。

import UIKit

class ViewController: UIViewController {
    
    
    var block1 = UIView()

    var btn = UIButton()
  
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        [block1,btn].forEach{
            [=11=].translatesAutoresizingMaskIntoConstraints = false
            view.addSubview([=11=])
        }
   
        block1.backgroundColor = .yellow
 
        btn.backgroundColor = .red
        
        block1.frame = CGRect(x: 0, y: 0, width: view.frame.width, height: view.frame.height * 0.1)
 
        
        btn.frame = CGRect(x: 100, y: 300, width: 100, height: 100)
        btn.backgroundColor = .red
        
        btn.addTarget(self, action: #selector(diverse), for: .touchDown)
    }
    
    @objc func diverse(){
        
        //1
        UIView.animate(withDuration: 1, animations: {
            
            self.block1.frame = CGRect(x: 100, y: 100, width: 100, height: 200)
            self.block1.center = self.view.center
            
        })
        //2
        { done in
            if done {
                UIView.animate(withDuration: 11, animations: {
                    
                    self.block1.frame = CGRect(x: 100, y: 100, width: 22, height: 100)
                    self.block1.center = self.view.center
                    
                })
                
                
                
                
                
            }
        }
        //3 does not work
        { done in
            if done {
                UIView.animate(withDuration: 11, animations: {
                    
                    self.block1.frame = CGRect(x: 100, y: 100, width: 2, height: 100)
                    self.block1.center = self.view.center
                    
                })
                
                
                
                
                
            }
        }
        
        
      
        

        
        
    }
    
    
    }

documentation for animate(withDuration:animations:completion:) 显示其声明,即:

class func animate(withDuration duration: TimeInterval, 
        animations: @escaping () -> Void, 
        completion: ((Bool) -> Void)? = nil)

completion 参数标签接受一个闭包,它在动画结束时被调用。

UIView.animate(withDuration: 1, animations: {
    self.block1.frame = CGRect(x: 100, y: 100, width: 100, height: 200)
    self.block1.center = self.view.center
}) { done in /// closure here

}

但是,第二个完成闭包没有参数标签。这就是为什么

UIView.animate(withDuration: 1, animations: {
    self.block1.frame = CGRect(x: 100, y: 100, width: 100, height: 200)
    self.block1.center = self.view.center
}) { done in /// closure here

} { done in /// second closure? Nope.

}

...不编译。

相反,你想要做的是

  • 将第二个动画放在第一个动画的完成处理程序中
  • 将第三个动画放在第二个的完成处理程序中
/// start first animation
UIView.animate(withDuration: 1, animations: {
    self.block1.frame = CGRect(x: 100, y: 100, width: 100, height: 200)
    self.block1.center = self.view.center
}) { done in

    /// first animation finished, start second
    if done {
        UIView.animate(withDuration: 11, animations: {
            
            self.block1.frame = CGRect(x: 100, y: 100, width: 22, height: 100)
            self.block1.center = self.view.center
            
        }) { done in

            /// second animation finished, start third
            if done {
                UIView.animate(withDuration: 11, animations: {
                    
                    self.block1.frame = CGRect(x: 100, y: 100, width: 2, height: 100)
                    self.block1.center = self.view.center
                    
                })
            }

        }
    }
}