用 3 个动作快速制作动画

问题描述

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

import UIKit

class ViewController: UIViewController {
    
    
    var block1 = UIView()

    var btn = UIButton()
  
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        [block1,btn].forEach{
            $0.translatesAutoresizingMaskIntoConstraints = false
            view.addSubview($0)
        }
   
        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,height: 200)
            self.block1.center = self.view.center
            
        })
        //2
        { done in
            if done {
                UIView.animate(withDuration: 11,animations: {
                    
                    self.block1.frame = CGRect(x: 100,width: 22,height: 100)
                    self.block1.center = self.view.center
                    
                })
                
                
                
                
                
            }
        }
        //3 does not work
        { done in
            if done {
                UIView.animate(withDuration: 11,width: 2,height: 100)
                    self.block1.center = self.view.center
                    
                })
                
                
                
                
                
            }
        }
        
        
      
        

        
        
    }
    
    
    }

解决方法

animate(withDuration:animations:completion:)documentation 显示了它的声明,它是这样的:

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,height: 200)
    self.block1.center = self.view.center
}) { done in /// closure here

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

}

... 无法编译。

相反,你想做的是

  • 将第二个动画放在第一个的完成处理程序中
  • 将第三个动画放在第二个的完成处理程序中
/// start first animation
UIView.animate(withDuration: 1,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,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,width: 2,height: 100)
                    self.block1.center = self.view.center
                    
                })
            }

        }
    }
}