iOS 9路径的中心位置随大小屏幕而变化

时间:2022-11-11 12:39:27

I would like to add a path in an UIView and i am doing like this :

我想在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)

That's work on iPhone 6 and 6S but when the screen is tiny or bigger, the circle doesn't have a good position :

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

iPhone 6S :

iPhone 6S:

iOS 9路径的中心位置随大小屏幕而变化

iPhone 5S :

iPhone 5S :

iOS 9路径的中心位置随大小屏幕而变化

Maybe an idea why ?

也许一个想法为什么?

Thanks

2 个解决方案

#1


1  

You probably draw the path at a moment where the autolayout constraints didn't kick in yet, so the drawing is made relative to the design-time view frame.

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

Two possible solutions:

两种可能的方案:

1) Draw the circle in a subview that is centered in the full view with autolayout constraints and always has the same size.

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

2) Redraw your path on any size change in viewDidLayoutSubviews.

2)在viewDidLayoutSubviews中重写任何大小更改的路径。

#2


0  

Probably you should use center parameter. I test this on different screen sizes and circle is directly on the middle of screen.

可能你应该使用中心参数。我在不同的屏幕尺寸上测试它,圆圈直接在屏幕中间。

 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)

#1


1  

You probably draw the path at a moment where the autolayout constraints didn't kick in yet, so the drawing is made relative to the design-time view frame.

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

Two possible solutions:

两种可能的方案:

1) Draw the circle in a subview that is centered in the full view with autolayout constraints and always has the same size.

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

2) Redraw your path on any size change in viewDidLayoutSubviews.

2)在viewDidLayoutSubviews中重写任何大小更改的路径。

#2


0  

Probably you should use center parameter. I test this on different screen sizes and circle is directly on the middle of screen.

可能你应该使用中心参数。我在不同的屏幕尺寸上测试它,圆圈直接在屏幕中间。

 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)