Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/19.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 使用CGRectMake Swift使椭圆形居中_Ios_Swift_Constraints_Cgrectmake - Fatal编程技术网

Ios 使用CGRectMake Swift使椭圆形居中

Ios 使用CGRectMake Swift使椭圆形居中,ios,swift,constraints,cgrectmake,Ios,Swift,Constraints,Cgrectmake,我在尝试以编程方式将椭圆居中时遇到问题,目前我有以下代码 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

我在尝试以编程方式将椭圆居中时遇到问题,目前我有以下代码

    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)
    }
这段代码能够在iPhone6和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)

您需要将层的锚点设置为其中心

oval.anchorPoint = CGPoint(x: 0.5, y: 0.5)
然后可以将图层的位置设置为视图的中心:

oval.position = self.center

感谢您的快速回复,我试图实现该代码,但由于某些原因,它仍然没有将椭圆居中。它仍在向一侧移动。