首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Swift stop CABasicAnimation

Swift stop CABasicAnimation
EN

Stack Overflow用户
提问于 2021-10-16 09:55:19
回答 1查看 56关注 0票数 1

Im使用CABasicAnimation制作像脉冲帧一样的动画视图

有代码

代码语言:javascript
复制
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中查看

代码语言:javascript
复制
func startPul() {
self.backPulseView.pulsate(animationDuration: 2.0)
}

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

代码语言:javascript
复制
func stopPul() {
self.backPulseView.removeAnimation()
}

但动画仍在运行且不会被删除

如何删除动画,然后才能再次调用?

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

代码语言:javascript
复制
 @IBAction func stopBtn(_ sender: UIButton) {
    stopPul()
 }
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-10-16 11:11:01

要移除遍历sublayers的动画并移除该"pulse“关键点的动画,请执行以下操作:

代码语言:javascript
复制
self.layer.sublayers?.forEach {
            $0.speed = 0
            $0.removeAllAnimations()
            $0.removeFromSuperlayer()
        }
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/69594479

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档