Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/16.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 设置UIBezierPath对象的位置_Ios_Swift_User Interface - Fatal编程技术网

Ios 设置UIBezierPath对象的位置

Ios 设置UIBezierPath对象的位置,ios,swift,user-interface,Ios,Swift,User Interface,我想创建一个半圆拱。我已经使用UIBezierPath实现了这一点。然而,我的问题是,我的拱门的位置不是我想要的位置 从上图中可以看到,我希望蓝色物体居中,但它向左倾斜。代码如下 @IBOutlet weak var logo_backdrop: UIImageView! override func viewDidLoad() { super.viewDidLoad() //Setting Login View Background let bgColor = UI

我想创建一个半圆拱。我已经使用UIBezierPath实现了这一点。然而,我的问题是,我的拱门的位置不是我想要的位置

从上图中可以看到,我希望蓝色物体居中,但它向左倾斜。代码如下

@IBOutlet weak var logo_backdrop: UIImageView!

override func viewDidLoad() {
    super.viewDidLoad()

    //Setting Login View Background
    let bgColor = UIColor(red: 128, green: 206, blue: 201)
    self.view.backgroundColor = bgColor

    //Setting Logo Backdrop color
    let backdrop = UIColor(red:183, green:227, blue:239)
    logo_backdrop.backgroundColor = backdrop

    //Setting Round Corner for Logo Backdrop
    let maskPath = UIBezierPath(roundedRect: logo_backdrop.bounds,
        byRoundingCorners: [.bottomLeft, .bottomRight],
        cornerRadii: CGSize(width: 100.0, height: 100.0))
    maskPath.move(to: CGPoint(x: view.frame.width, y: 0.0))

    let shape = CAShapeLayer()
    shape.frame = logo_backdrop.bounds
    shape.path = maskPath.cgPath
    logo_backdrop.layer.mask = shape
}

一个明显的问题是您的代码位于
viewDidLoad
中。这还为时过早,因为各种观点还没有形成真正的规模。但您的代码取决于该大小,因此不能过早运行


例如,您的贝塞尔路径取决于
logo\u background.bounds
,但这些边界正是我们在
viewdiload

中不知道的,显而易见的地方是
viewdilayoutsubviews
。但是要小心,因为该方法被多次调用,而您只想在第一次执行此操作。viewDidLayoutSubviews何时再次调用?根据您的建议,有什么更好的方法可以做到这一点?我所做的是使用Bool属性,以便只运行一次配置代码。