ios - swift中uiview的定位层

标签 ios swift uiview draw cglayer

我正在处理时间线 View 。我在图标中心画一条线,并用填充颜色画一个圆圈。但问题是,当我绘制圆圈时,它始终位于图标的顶部。现在图标不显示。我尝试了图层的 zposition 。这是我尝试过的

    override func draw(_ rect: CGRect) {
            if let anchor =  anchorView(){
                let centreRelativeToTableView = anchor.superview!.convert(anchor.center, to: self)
               // print("Anchor x origin : \(anchor.frame.size.origin.x)")
                 timelinePoint.position = CGPoint(x: centreRelativeToTableView.x , y: centreRelativeToTableView.y/2)
                timeline.start = CGPoint(x: centreRelativeToTableView.x  , y: 0)
                timeline.middle = CGPoint(x: timeline.start.x, y: anchor.frame.origin.y)
                timeline.end = CGPoint(x: timeline.start.x, y: self.bounds.size.height)
                timeline.draw(view: self.contentView)




                let circlePath = UIBezierPath(arcCenter: CGPoint(x: anchor.center.x,y: anchor.center.y - 20), radius: CGFloat(20), startAngle: CGFloat(0), endAngle:CGFloat(M_PI * 2), clockwise: true)

                let shapeLayer = CAShapeLayer()
                shapeLayer.path = circlePath.cgPath

                //change the fill color
                shapeLayer.fillColor = UIColor.randomFlat.cgColor
               // shapeLayer.fillColor = UIColor.clear.cgColor

                //you can change the stroke color
                shapeLayer.strokeColor = UIColor.white.cgColor
                //you can change the line width
                shapeLayer.lineWidth = 5
                shapeLayer.zPosition = 0
                anchor.alpha = 1
                //Set Anchor Z position
                  anchor.layer.zPosition = 2
                shapeLayer.zPosition = 1

                anchor.layer.addSublayer(shapeLayer)


                // Setting icon to layer
                /*let imageLayer = CALayer()
                imageLayer.contents = newImage
                anchor.layer.addSublayer(imageLayer)*/
               // timelinePoint.draw(view: self.contentView)
            }else{
                print("this should not happen")
            }


}

ss1 -------- s2

我想在圆圈顶部绘制带有白色色调的图标。请帮助我

最佳答案

使用 addSublayer 将在顶部添加新层,这将覆盖之前添加的所有其他内容。

如果您知道您的图标位于哪一层,您可以使用 insertSublayer(at:) 将其放置在您的图标下方

或者,您可以使用 Storyboard 中图标的精确框架创建另一个 UIView,并将其放置在层次结构中的较低位置,以便您绘制的内容最终位于下面。

关于ios - swift中uiview的定位层,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41157718/

相关文章:

javascript - Hammer.js 平移事件仅适用于触摸设备,不适用于桌面计算机单击+拖动

ios - 停止 UI 警报 Controller 返回到上一个 View

ios - 不同的启动图像 iPhone 6 和 iPhone 4s

ios - 将曲线添加到条形图

ios - 如何使用iOS SDK授权和登录微信?

swift - 在一个上下文中绘制实线和虚线

swift - 为什么 `required` 不是 Swift 中类函数的有效关键字?

ios - 如何在 Swift 2.3 中将 UInt8 转换为 Anyobject

ios - 拖动 UIView 结束时会丢失手势

ios - 在 Swift 中用另一个实例替换 UIView 的 self