【问题标题】:Swift stop CABasicAnimation迅速停止 CABasicAnimation
【发布时间】:2021-10-16 09:55:18
【问题描述】:

我将 CABasicAnimation 用于像脉冲帧这样的动画视图

有代码

extension UIView {
/// animate the border width
func animateBorderWidth(toValue: CGFloat, duration: Double) -> CABasicAnimation {
    let widthAnimation = CABasicAnimation(keyPath: "borderWidth")
    widthAnimation.fromValue = 0.9
    widthAnimation.toValue = 0.0
    widthAnimation.duration = 15
    widthAnimation.timingFunction = CAMediaTimingFunction(name: .easeOut)
    return widthAnimation
}

/// animate the scale
func animateScale(toValue: CGFloat, duration: Double) -> CABasicAnimation {
    let scaleAnimation = CABasicAnimation(keyPath: "transform.scale")
    scaleAnimation.fromValue = 1.0
    scaleAnimation.toValue = 12
    scaleAnimation.duration = 55
    scaleAnimation.repeatCount = .infinity
    scaleAnimation.isRemovedOnCompletion = false
    scaleAnimation.timingFunction = CAMediaTimingFunction(name: .linear)
    return scaleAnimation
}
    
func pulsate(animationDuration: CGFloat) {
    var animationGroup: CAAnimationGroup!
    let newLayer = CALayer()
    animationGroup = CAAnimationGroup()
    animationGroup.duration = CFTimeInterval(animationDuration)
    animationGroup.repeatCount = Float.infinity
    
    newLayer.bounds = CGRect(x: 0, y: 0, width: self.frame.width, height: self.frame.height)
    newLayer.cornerRadius = self.frame.width/2
    newLayer.position = CGPoint(x: self.frame.width/2, y: self.frame.height/2)
    newLayer.cornerRadius = self.frame.width/2
    newLayer.borderColor = #colorLiteral(red: 0.7215686275, green: 0.5137254902, blue: 0.7647058824, alpha: 0.7).cgColor
    
        animationGroup.timingFunction = CAMediaTimingFunction(name: .easeOut)
    animationGroup.animations = [animateScale(toValue: 7.5, duration: 2.0),
                                 animateBorderWidth(toValue: 0.6, duration: Double(animationDuration))]
        animationGroup.duration = 14
    newLayer.add(animationGroup, forKey: "pulse")
    self.layer.cornerRadius = self.frame.width/2
    self.layer.insertSublayer(newLayer, at: 0)
}

func removeAnimation() {
    self.layer.sublayers?.forEach { $0.removeAnimation(forKey: "pulse") }
}

}

并在 UIViewCONtroller 中添加此动画以查看

func startPul() {
self.backPulseView.pulsate(animationDuration: 2.0)
}

但是如果我按下按钮我想停止动画并隐藏 我正在尝试

func stopPul() {
self.backPulseView.removeAnimation()
}

但是动画仍然有效并且没有被移除

如何删除动画然后我可以再次调用?

停止我从按钮点击调用的动画

 @IBAction func stopBtn(_ sender: UIButton) {
    stopPul()
 }

【问题讨论】:

  • 检查暂停和恢复动画。 stackoverflow.com/a/43909370/14733292
  • @RajaKishan 没有帮助这个暂停功能
  • 你能最小化示例代码吗?还有 GIF 还是视频?
  • @RajaKishan 添加 gif 和函数

标签: swift animation cabasicanimation


【解决方案1】:

要删除动画,遍历sublayers 并删除那个“脉冲”键的动画:

self.layer.sublayers?.forEach {
            $0.speed = 0
            $0.removeAllAnimations()
            $0.removeFromSuperlayer()
        }
}

【讨论】:

  • 我在脉动函数下面添加了,当我需要删除调用 self.backPul​​seView.removeAnimation() 时没有任何反应
  • @Oleg 你在stopPul 中添加了self.backPulseView.removeAnimation() 吗?什么时候调用 stopPul 方法??
  • 不,我改变这个我按下视图控制器上的按钮(动画放置在哪里)并调用函数 func stopPul() { self.backPul​​seView.removeAnimation() }
  • 尝试 self.layer.sublayers?.forEach { $0.removeAllAnimations() } 并检查它现在是否有效。
  • 超级!谢谢你的帮助!!
猜你喜欢
  • 1970-01-01
  • 2012-11-10
  • 1970-01-01
  • 2011-07-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多