设置 UIBezierPath 对象的位置 [英] Setting the Location of UIBezierPath Object

查看:25
本文介绍了设置 UIBezierPath 对象的位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I want to create a semi-circle arch. I have achieved that using UIBezierPath. However, my issue is that the location of my arch is not where I want it to be.

如上图所示,我希望蓝色对象居中,但它偏向左侧.代码如下.

As you can see from the image above, I want the blue object to be centred, but it is leaning to the left. Code below.

@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 中.这为时过早,因为各种视图还没有达到它们的实际大小.但是您的代码取决于该大小,因此不能过早运行.

One obvious problem is that your code is in viewDidLoad. That is too soon, as the various views have not yet taken on their real size. But your code depends upon that size, so it must not be run too early.

例如,您的贝塞尔曲线取决于 logo_backdrop.bounds,但这些边界正是我们在 viewDidLoad 中所不知道的.

For example, your bezier path depends upon logo_backdrop.bounds, but those bounds are exactly what we do not know in viewDidLoad.

这篇关于设置 UIBezierPath 对象的位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆