【问题标题】:do a animation with 3 actions in swift用 3 个动作快速制作动画
【发布时间】:2021-04-13 01:37:04
【问题描述】:

我希望我的 swift 代码使用单独和不同的动作来制作动画。那是按顺序排列的。前 2 个动画有效,但是当我添加第 3 个动画时,它不再有效并导致编译错误。您可以看到评论所在的第三个动画。我只想让所有 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, 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
                    
                })
                
                
                
                
                
            }
        }
        
        
      
        

        
        
    }
    
    
    }

【问题讨论】:

    标签: swift animation jquery-animate cgrect completion


    【解决方案1】:

    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, 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
                        
                    })
                }
    
            }
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-08-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-02-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多