swift - CAShapelayer 副本

标签 swift cashapelayer cabasicanimation

我正在尝试每 20 毫秒向给定的 x 和 y 坐标添加一次 CAShapelayer。我希望形状在一秒钟内消失(就像示踪剂一样)。我创建的功能有效,形状在正确的位置创建并消失。但是我留下了额外的形状,使屏幕变得杂乱无章。

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")

}

最佳答案

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

您还可以将图层的fillMode设置为kCAFillModeForwards,并将removedOnCompletion设置为false,这将有图层保留其“动画结束”状态。但我个人只会设置 strokeColor 如上所述,因为使用 trueremovedOnCompletion 会干扰 animationDidStop(请参阅下)。


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

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?

关于swift - CAShapelayer 副本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37975849/

相关文章:

swift - 如何使用按钮和文本字段移动到其他 View Controller ?

ios - 按下主页按钮后 NSTimer 不触发

ios - 检测变换后的 CAShapeLayer 路径上的点击

ios - CoreAnimation — 在动画开始之前图层闪烁到最终值?

ios - 如何从 View Controller 访问 AppDelegate 中声明的导航 Controller ?

ios - 在 Swift 中单击 CAShapeLayer 时检测触摸

ios - 带有 UIRectCornerBottomLeft/Right 的 CAShapeLayer 使我的 View 消失

ios - 使用 Swift 动画循环进度

core-animation - 带有CALayer路径的CABasicAnimation不动画

swift - 学习类型方法/类型属性