iOS 9 路径中心位置随尺寸屏幕变化

iOS 9 center position of path change with size screen

我想在 UIView 中添加一个路径,我是这样做的:

let Circle = UIBezierPath(arcCenter: CGPoint(x: InterfaceView.bounds.size.width/2, y: InterfaceView.bounds.size.height/2), radius: 10, startAngle: 0, endAngle: CGFloat(M_PI*2), clockwise: true)
let Layer = CAShapeLayer()
Layer.path = Circle.CGPath
Layer.strokeColor = UIColor.redColor().CGColor
Layer.lineWidth = 1.0
InterfaceView.layer.addSublayer(Layer)

这适用于 iPhone 6 和 6S,但当屏幕很小或更大时,圆圈的位置不佳:

iPhone 6S :

iPhone 5S :

也许知道为什么?

谢谢

您可能在自动布局约束尚未启动的时刻绘制路径,因此绘图是相对于设计时视图框架绘制的。

两种可能的解决方案:

1) 在具有自动布局约束的全视图居中的子视图中绘制圆,并且始终具有相同的大小。

2) viewDidLayoutSubviews.

中任何尺寸变化时重绘路径

也许你应该使用 center 参数。我在不同的屏幕尺寸上测试了这个,圆圈直接在屏幕中间。

 let Circle = UIBezierPath(arcCenter: self.view.center, radius: 10, startAngle: 0, endAngle: CGFloat(M_PI*2), clockwise: true)
        let Layer = CAShapeLayer()
        Layer.path = Circle.CGPath
        Layer.strokeColor = UIColor.redColor().CGColor
        Layer.lineWidth = 1.0
        self.view.layer.addSublayer(Layer)