不能在不同的iPhone设备上放置围绕imageView的圆形图层

时间:2018-01-17 12:00:28

标签: ios swift uiimageview layer

enter image description here

如何解决此问题? 图像在故事板中设置了约束;另一个圆圈是用这些代码行设置的:

building_id

在iPhone 7上(右边的那个)似乎是正确的。

4 个答案:

答案 0 :(得分:1)

使用固定值导航或状态'条形高度将导致此类错误。您应该根据图像视图定位图层。为此,您只需将图像视图的帧分配给您的图层:

reference

由于布局可能会因多种原因(例如设备旋转)而发生变化,因此您应该在视图控制器的trackLayer.frame = profileImage.frame 中执行此操作。

由于图层的框架现在小于示例中的框架,因此您应该使用以下内容创建中心:

viewDidLayoutSubviews

答案 1 :(得分:1)

override func viewDidLayoutSubviews() {

    let centerY = profileImage.center.y+20+(self.navigationController?.navigationBar.frame.size.height)!
    let centerX = profileImage.center.x
    let center = CGPoint(x: centerX, y: centerY)
    let trackLayer = CAShapeLayer()
    let circularPath = UIBezierPath(arcCenter: center, radius: (profileImage.frame.width/2)+2, startAngle: -CGFloat.pi-CGFloat.pi/2, endAngle: CGFloat.pi/2, clockwise: true)
    trackLayer.path = circularPath.cgPath
    trackLayer.strokeColor = Functions.hexStringToUIColor(hex: "#3859B9").cgColor
    trackLayer.lineWidth = 3
    trackLayer.lineCap = kCALineCapRound
    trackLayer.fillColor = UIColor.clear.cgColor
    view.layer.addSublayer(trackLayer)

 }

或者在没有延迟的情况下在viewDidAppear中编写代码

答案 2 :(得分:0)

这可以解决问题。

  1. layer添加到profileImage.layer
  2. trackLayer.frame = profileImage.bounds
  3. 完整代码应该是这样的:

    override func viewDidLayoutSubviews() {
        super.viewDidLayoutSubviews()
        let center = CGPoint(x: profileImage.bounds.midX,
                             y: profileImage.bounds.midY)
        let trackLayer = CAShapeLayer()
        trackLayer.frame = profileImage.frame
        let circularPath = UIBezierPath(arcCenter: center,
                                        radius: (profileImage.frame.width / 2) + 2,
                                        startAngle: -CGFloat.pi - CGFloat.pi / 2,
                                        endAngle: CGFloat.pi / 2, clockwise: true)
        trackLayer.path = circularPath.cgPath
        trackLayer.strokeColor = Functions.hexStringToUIColor(hex: "#3859B9").cgColor
        trackLayer.lineWidth = 3
        trackLayer.lineCap = kCALineCapRound
        trackLayer.fillColor = UIColor.clear.cgColor
        profileImage.layer.addSublayer(trackLayer)
    }
    

答案 3 :(得分:0)

使用界面构建器:

  1. 拖动UIView并将其放在imageview
  2. 上方
  3. 将视图自定义类更改为CirculerProgressView
  4. 使用imageview center
  5. 设置中心X和Y约束
  6. 设置高度和宽度限制(大于imageView高度和宽度)

    @IBDesignable class CirculerProgressView: UIView {
    
                override class var layerClass: AnyClass {
                    return CAShapeLayer.self
                }
    
                override func draw(_ rect: CGRect) {
                    super.draw(rect)
                    setupLayer()
                }
    
                private func setupLayer() {
                    let trackLayer = layer as! CAShapeLayer
                    let center = CGPoint(x: frame.width/2, y: frame.height/2)
                    let circularPath = UIBezierPath(arcCenter: center, radius: frame.width/2, startAngle: -CGFloat.pi-CGFloat.pi/2, endAngle: CGFloat.pi/2, clockwise: true)
                    trackLayer.path = circularPath.cgPath
                    trackLayer.strokeColor = UIColor.blue.cgColor
                    trackLayer.lineWidth = 3
                    trackLayer.lineCap = kCALineCapRound
                    trackLayer.fillColor = UIColor.clear.cgColor
                }}