【问题标题】:CABasicAnimation performance before removeFromSuperView()removeFromSuperView() 之前的 CABasicAnimation 性能
【发布时间】:2020-08-31 14:50:07
【问题描述】:

我有一个代码创建的按钮。每次单击时,我希望它在 removeFromSuperView() 之前执行 CABasicAnimation。但是如果我在那之后添加 removeFromSuperView() ,它会立即 removeFromSuperView() 而没有任何动画。

@objc func bubblePressed(_ bubble:Bubble){
    let animation = CABasicAnimation(keyPath:"opacity")
    animation.fromValue = 1
    animation.toValue = 0
    animation.duration = 2.0
    bubble.layer.add(animation, forKey:nil)
    bubble.removeFromSuperview()
}

有什么方法可以实现吗?

【问题讨论】:

    标签: ios swift cabasicanimation removefromsuperview


    【解决方案1】:
    @objc func bubblePressed(_ bubble:Bubble){
        CATransaction.begin()
        CATransaction.setCompletionBlock({
            // remove from super view
          bubble.removeFromSuperview()
        })
        let animation = CABasicAnimation(keyPath:"opacity")
        animation.fromValue = 1
        animation.toValue = 0
        animation.duration = 2.0
        bubble.layer.add(animation, forKey:nil)
        CATransaction.commit()
    
    }
    

    解决方法 2

         @objc func bubblePressed(_ bubble:Bubble){
    
                    let animation = CABasicAnimation(keyPath:"opacity")
                    animation.fromValue = 1
                    animation.toValue = 0
                    animation.duration = 2.0
                    animatio.delegate = self
                    bubble.layer.add(animation, forKey:nil) 
                }
    
      extension ViewController: CAAnimationDelegate {
    
         func animationDidStop(_ anim: CAAnimation,  finished flag: Bool) {
            bubble.removeFromSuperview()
    
          // remove from super view in this function .. its Delegate method
                    }
               }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-07-04
      • 2012-09-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-12-03
      相关资源
      最近更新 更多