更改子视图层的位置

时间:2018-11-20 18:58:45

标签: xcode uiview layer zposition

晚上好,

我一直想弄清楚如何在按下按钮时使屏幕褪色,以使子视图具有更鲜明的外观。我研究了zPositions,并认为在我的代码中实现此功能会有所帮助。当我运行代码时,整个屏幕都会消失。我希望在背景渐淡时出现一个圆圈进度环。我将显示图片和代码。预先谢谢你。

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)

}

1 个答案:

答案 0 :(得分:0)

花了几个小时重新编写代码之后,我设法弄出了自己的答案。 之前

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

我只需要添加

    self.view.addSubview(blackScreen)

将其转换为一层,可以使用zPosition对其进行操作。