user9951119
user9951119

Reputation:

Change subview layer position

Good evening,

I have been trying to figure out how to fade a screen when I press a button in order to give a subview a more distinct appearance. I have researched zPositions and I thought that implementing this feature within my code would help. When I run my code, the entire screen fades. I would like there to be a circle progress ring that shows up, while the background is faded. I will show a picture and my code. Thank you in advance.

The circle ring, I would like to be abover the faded screen.

 @objc func handlePost() {
    let center = view.center
    let trackLayer = CAShapeLayer()
    let circularPath = UIBezierPath(arcCenter: center, radius: 100, startAngle: -CGFloat.pi / 2, endAngle: 2 * CGFloat.pi, clockwise: true)
    blackScreen=UIView(frame: self.view.bounds)
    blackScreen.backgroundColor=UIColor(white: 0, alpha: 0.8)
    blackScreen.isHidden=true
    self.navigationController?.view.addSubview(blackScreen)
    blackScreen.layer.zPosition=50
    blackScreen.isHidden=false
    self.view.backgroundColor = UIColor.white
    trackLayer.path = circularPath.cgPath
    trackLayer.strokeColor = UIColor.lightGray.cgColor
    trackLayer.lineWidth = 10
    trackLayer.fillColor = UIColor.clear.cgColor
    trackLayer.lineCap = kCALineCapRound
    // The zPosition is supposed to bring this layer to the front. 
    trackLayer.zPosition = 100
    shapeLayer.path = circularPath.cgPath
    shapeLayer.strokeColor = GREEN_Theme.cgColor
    shapeLayer.lineWidth = 10
    shapeLayer.fillColor = UIColor.clear.cgColor
    shapeLayer.lineCap = kCALineCapRound
    shapeLayer.strokeEnd = 0
    // The zPosition is supposed to bring this layer to the front. 
    shapeLayer.zPosition = 100
    let basicAnimation = CABasicAnimation(keyPath: "strokeEnd")
    basicAnimation.toValue = 1
    basicAnimation.duration = 2
    basicAnimation.fillMode = kCAFillModeForwards
    basicAnimation.isRemovedOnCompletion = false
    shapeLayer.add(basicAnimation, forKey: "dataSearch")
    shapeLayer.removeAnimation(forKey: "finished")
    view.layer.addSublayer(trackLayer)
    view.layer.addSublayer(shapeLayer)

}

Upvotes: 0

Views: 224

Answers (1)

user9951119
user9951119

Reputation:

Again after spending hours with my code, I managed to figure out my own answer. before I had

     self.navigationController?.view.addSubview(blackScreen)
  // (faded screen)

I simply needed to add

    self.view.addSubview(blackScreen)

to turn it into a layer that way I could manipulate it with my zPosition.

Upvotes: 1

Related Questions