为什么绘制的UIBezierPath 位于右下方,而没有指定CAShapeLayer 的中心位置?
Why is the drawn UIBezierPath positioned in the bottom right without specifying a central position in CAShapeLayer?
我试图将 UIBezierPath
集中在超级视图中,但是 UIBezierPath
位于右下角,而不是中心。
我把anchorPoint
的值设为(0.5, 0.5)
、position
、self.view.center
或self.view.frame.width / 2
,但是UIBezierPath
没有居中。
import UIKit
/* ViewSection */
class ViewController: UIViewController {
@IBOutlet weak var Toolbar: UIToolbar!
@IBOutlet weak var UIToolbarButtonItem: UIBarButtonItem!
@IBAction func UIToolbarButtonItem(_ sender: Any) {
let objectFramePath = UIBezierPath.init(roundedRect: CGRect(x: 0, y: 0, width: 150, height: 150), byRoundingCorners: .allCorners, cornerRadii: CGSize(width: 39.5, height: 39.5))
let shapeLayer = CAShapeLayer()
shapeLayer.anchorPoint = CGPoint(x: 0.5, y: 0.5)
shapeLayer.position = self.view.center
shapeLayer.fillColor = UIColor.blue.cgColor
shapeLayer.path = objectFramePath.cgPath
self.view.layer.addSublayer(shapeLayer)
}
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}
}
结果是这样的:
锚点不起作用,因为新层没有frame
。如果你设置它,它会起作用:
let rect = CGRect(x: 0, y: 0, width: 150, height: 150)
let path = UIBezierPath(roundedRect: rect, byRoundingCorners: .allCorners, cornerRadii: CGSize(width: 39.5, height: 39.5))
let shapeLayer = CAShapeLayer()
shapeLayer.anchorPoint = CGPoint(x: 0.5, y: 0.5)
shapeLayer.frame = rect
shapeLayer.position = CGPoint(x: view.bounds.midX, y: view.bounds.midY)
shapeLayer.fillColor = UIColor.blue.cgColor
shapeLayer.path = path.cgPath
view.layer.addSublayer(shapeLayer)
或者,您可以只设置 path
的原点:
let size = CGSize(width: 150, height: 150)
let origin = CGPoint(x: view.bounds.midX - size.width / 2,
y: view.bounds.midY - size.height / 2)
let path = UIBezierPath(roundedRect: CGRect(origin: origin, size: size), byRoundingCorners: .allCorners, cornerRadii: CGSize(width: 39.5, height: 39.5))
let shapeLayer = CAShapeLayer()
shapeLayer.fillColor = UIColor.blue.cgColor
shapeLayer.path = path.cgPath
view.layer.addSublayer(shapeLayer)
顺便说一下,注意我没有引用 center
,因为 as the docs 说,“中心点在其父视图的坐标系中以点指定。”
总是使用 bounds
(如果你需要它的中心,bounds
的 midX
和 midY
),因为它总是在相关视图的坐标系。在这种情况下,它恰好没有太大的区别,但我们应该注意到 superview 中的 bounds
和 center
的中间是两个完全不同的东西。
我试图将 UIBezierPath
集中在超级视图中,但是 UIBezierPath
位于右下角,而不是中心。
我把anchorPoint
的值设为(0.5, 0.5)
、position
、self.view.center
或self.view.frame.width / 2
,但是UIBezierPath
没有居中。
import UIKit
/* ViewSection */
class ViewController: UIViewController {
@IBOutlet weak var Toolbar: UIToolbar!
@IBOutlet weak var UIToolbarButtonItem: UIBarButtonItem!
@IBAction func UIToolbarButtonItem(_ sender: Any) {
let objectFramePath = UIBezierPath.init(roundedRect: CGRect(x: 0, y: 0, width: 150, height: 150), byRoundingCorners: .allCorners, cornerRadii: CGSize(width: 39.5, height: 39.5))
let shapeLayer = CAShapeLayer()
shapeLayer.anchorPoint = CGPoint(x: 0.5, y: 0.5)
shapeLayer.position = self.view.center
shapeLayer.fillColor = UIColor.blue.cgColor
shapeLayer.path = objectFramePath.cgPath
self.view.layer.addSublayer(shapeLayer)
}
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}
}
结果是这样的:
锚点不起作用,因为新层没有frame
。如果你设置它,它会起作用:
let rect = CGRect(x: 0, y: 0, width: 150, height: 150)
let path = UIBezierPath(roundedRect: rect, byRoundingCorners: .allCorners, cornerRadii: CGSize(width: 39.5, height: 39.5))
let shapeLayer = CAShapeLayer()
shapeLayer.anchorPoint = CGPoint(x: 0.5, y: 0.5)
shapeLayer.frame = rect
shapeLayer.position = CGPoint(x: view.bounds.midX, y: view.bounds.midY)
shapeLayer.fillColor = UIColor.blue.cgColor
shapeLayer.path = path.cgPath
view.layer.addSublayer(shapeLayer)
或者,您可以只设置 path
的原点:
let size = CGSize(width: 150, height: 150)
let origin = CGPoint(x: view.bounds.midX - size.width / 2,
y: view.bounds.midY - size.height / 2)
let path = UIBezierPath(roundedRect: CGRect(origin: origin, size: size), byRoundingCorners: .allCorners, cornerRadii: CGSize(width: 39.5, height: 39.5))
let shapeLayer = CAShapeLayer()
shapeLayer.fillColor = UIColor.blue.cgColor
shapeLayer.path = path.cgPath
view.layer.addSublayer(shapeLayer)
顺便说一下,注意我没有引用 center
,因为 as the docs 说,“中心点在其父视图的坐标系中以点指定。”
总是使用 bounds
(如果你需要它的中心,bounds
的 midX
和 midY
),因为它总是在相关视图的坐标系。在这种情况下,它恰好没有太大的区别,但我们应该注意到 superview 中的 bounds
和 center
的中间是两个完全不同的东西。