首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >CAShapelayer重复

CAShapelayer重复
EN

Stack Overflow用户
提问于 2016-06-22 18:36:05
回答 1查看 155关注 0票数 1

我试图在给定的x和y坐标中每20 to添加一次CAShapelayer。我希望这个形状在一秒内消失(就像一个示踪剂)。我创建的函数作品,形状是创建在正确的位置和消失。但我的屏幕上有多余的形状。

代码语言:javascript
复制
func shadowBall (x: CGFloat, y: CGFloat){

    let xpos : CGFloat = ((self.frame.width/2) + x)
    let ypos : CGFloat = ((self.frame.height/2) + y)

    let shadowBall = CAShapeLayer()
    let shadowBalllRadius :CGFloat = 4
    let shadowBallPath : UIBezierPath = UIBezierPath(ovalInRect: CGRect(x: xpos, y: ypos, width: CGFloat(shadowBalllRadius*2), height: CGFloat(shadowBalllRadius*2)))

    shadowBall.path = shadowBallPath.CGPath
    shadowBall.fillColor = UIColor.clearColor().CGColor
    shadowBall.strokeColor = UIColor.whiteColor().CGColor
    shadowBall.lineWidth = 0.5

    let animation: CABasicAnimation = CABasicAnimation(keyPath: "strokeColor")
    animation.fromValue = UIColor.whiteColor().CGColor
    animation.toValue = UIColor.clearColor().CGColor
    animation.duration = 1.0;
    animation.repeatCount = 0;
    animation.removedOnCompletion = true
    animation.additive = false

    self.layer.addSublayer(shadowBall)
    shadowBall.addAnimation(animation, forKey: "strokeColor")

}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2016-06-22 19:13:24

问题是,当动画完成后,它会将strokeColor恢复到原来的颜色。您应该将原始形状层的strokeColor设置为clearColor(),这样,当您完成从whiteColor()clearColor()的动画时,它将保留在clearColor()上。

您还可以将图层的fillMode设置为kCAFillModeForwards,将removedOnCompletion设置为false,这将使图层保留其“结束动画”状态。但我个人只想设置上面概述的strokeColor,因为使用removedOnCompletion of true会干扰animationDidStop (见下文)。

此外,我可能建议您也删除图层,一旦它完成了动画,这样它就不会继续消耗内存,尽管它不再可见。

代码语言:javascript
复制
func shadowBall (x: CGFloat, y: CGFloat) {
    let xpos: CGFloat = ((self.frame.width/2) + x)
    let ypos: CGFloat = ((self.frame.height/2) + y)

    let shadowBall = CAShapeLayer()
    let shadowBalllRadius: CGFloat = 4
    let shadowBallPath = UIBezierPath(ovalInRect: CGRect(x: xpos, y: ypos, width: CGFloat(shadowBalllRadius*2), height: CGFloat(shadowBalllRadius*2)))

    shadowBall.path = shadowBallPath.CGPath
    shadowBall.fillColor = UIColor.clearColor().CGColor
    shadowBall.strokeColor = UIColor.clearColor().CGColor
    shadowBall.lineWidth = 0.1

    let animation = CABasicAnimation(keyPath: "strokeColor")
    animation.fromValue = UIColor.whiteColor().CGColor
    animation.toValue = UIColor.clearColor().CGColor
    animation.duration = 1.0
    animation.repeatCount = 0
    animation.removedOnCompletion = true
    animation.additive = false

    animation.delegate = self
    animation.setValue(shadowBall, forKey: "animationLayer")

    self.layer.addSublayer(shadowBall)
    shadowBall.addAnimation(animation, forKey: "strokeColor")
}

override func animationDidStop(anim: CAAnimation, finished flag: Bool) {
    if let layer = anim.valueForKey("animationLayer") as? CALayer {
        layer.removeFromSuperlayer()
    }
}

请参阅How to remove a CALayer-object from animationDidStop?

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/37975849

复制
相关文章

相似问题

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