我目前无法尝试以编程方式使我的椭圆居中
func setupLayers(){
let oval = CAShapeLayer()
oval.frame = CGRectMake(137.5, 283.5, 100, 100)
oval.path = ovalPath().CGPath;
self.layer.addSublayer(oval)
layers["oval"] = oval
resetLayerPropertiesForLayerIdentifiers(nil)
}
该代码能够在iPhone 6和6s中使椭圆居中,但我希望它能够在所有设备中居中。
我也尝试过此代码:
func drawCircle(size: CGFloat) {
let circleShape = CAShapeLayer()
circleShape.path = UIBezierPath(ovalInRect: CGRectMake(-size/2, -size/2, size, size)).CGPath
circleShape.fillColor = UIColor.blueColor().CGColor
circleShape.strokeColor = UIColor.redColor().CGColor
circleShape.lineWidth = 1
circleShape.frame.origin = view.center
circleShape.name = "circleShape"
view.layer.addSublayer(circleShape)
}
drawCircle(100)
最佳答案
您需要将图层的anchorPoint设置为其中心。
oval.anchorPoint = CGPoint(x: 0.5, y: 0.5)
然后,您可以将图层的位置设置为视图中心:
oval.position = self.center