How to hide shapelayer (swift4)

My code below needs to hide a shapeLayer. Right now it removes that color that animates it but its border is still there. My problem can be fond in ////WHAT I AM TRYING IS NOT WORKING.the issue is obviously the tracking layer I dont know how to hide that .

                   let shapeLayer = CAShapeLayer()

   @objc func action(){
     time += 1
       lzbel.text = String(time)

if time == 60 {
    timer.invalidate()
    lzbel.isHidden = true
     enterText.isHidden = false
    enterScore.isHidden = false
    resetbtn.isHidden = false
    scores.isHidden = false
    ////WHAT I AM TRYING IS NOT WORKING
    shapeLayer.removeAllAnimations()
    shapeLayer.removeFromSuperlayer()


}

if time == 0 {
    izmge.image = UIImage(named:"art.png")
    lzbel.isHidden = true
    let center = CGPoint(x:view.center.x,y:view.center.y + 150)
    let trackLayer = CAShapeLayer()

    let circularPath = UIBezierPath(arcCenter: center, radius: 100, startAngle: -CGFloat.pi / 2, endAngle: 2 * CGFloat.pi, clockwise: true)

    trackLayer.path = circularPath.cgPath
    trackLayer.strokeColor = UIColor.lightGray.cgColor
    trackLayer.lineWidth = 10

    trackLayer.fillColor = UIColor.clear.cgColor
    trackLayer.lineCap = kCALineCapRound
    view.layer.addSublayer(trackLayer)




    shapeLayer.path = circularPath.cgPath

    shapeLayer.strokeColor = UIColor.red.cgColor
    shapeLayer.lineWidth = 10

    shapeLayer.fillColor = UIColor.clear.cgColor
    shapeLayer.lineCap = kCALineCapRound

    shapeLayer.strokeEnd = 0
    let basicAnimation = CABasicAnimation(keyPath: "strokeEnd")

    basicAnimation.toValue = 0.8
    basicAnimation.duration = 60

    basicAnimation.fillMode = kCAFillModeForwards
    basicAnimation.isRemovedOnCompletion = false
    shapeLayer.add(basicAnimation, forKey: "urSoBasice")

    view.layer.addSublayer(shapeLayer)


}}

Hi @timswift,
just an observation, you are stopping the animation on shapeLayer and when that happens, the trackLayer remains, is that the problem?

Have you tried to add

trackLayer.removeFromSuperlayer()

after you remove the shapeLayer from superview? It would thereby remove both the layers.

cheers,

Jayant

@jayantvarma Thanks for your response. The problem is, is that in if time == 60 {} track layer is not recognized, because it is declared in if time == 0{}. I dont know how I can recognize it outside of its it statement. The other thing to try is maybe when the animation duration is completed the track layer is hidden. But I dont know how to do that.

Hi @timswift,
have you tried to declare the trackLayer outside, just like the shapeLayer so that you can access it later

cheers,

Jayant

This topic was automatically closed after 166 days. New replies are no longer allowed.