[英]How do I position a CALayer in the center of a UIView in Swift?
I have this CALayer thats a dot that I want to be exactly at the center of the UIView.我有这个 CALayer,它是一个点,我想正好位于 UIView 的中心。 I have a UIView that covers 2/3 of the screen.
我有一个覆盖 2/3 屏幕的 UIView。 How would I get the dot to be in the middle of the UIView.
我如何让点位于 UIView 的中间。 The code below positions the dot an inch above where I need it to be.
下面的代码将点定位在我需要的位置上方一英寸处。 Here is my code:
这是我的代码:
func setupUI() {
let lineShape1 = CAShapeLayer()
let linePath1 = UIBezierPath.init(ovalIn: CGRect.init(x: 0, y: 0, width: 5, height: 5))
lineShape1.path = linePath1.cgPath
lineShape1.fillColor = UIColor.init(white: 0.7, alpha: 0.5).cgColor
lineShape1.position = CGPoint(x: cameraView.frame.size.width / 2, y: cameraView.frame.size.height / 2)
self.view.layer.addSublayer(lineShape1)
}
You are confusing frame and bounds.你混淆了框架和界限。 Set it like this
设置成这样
func setupUI() {
let lineShape1 = CAShapeLayer()
let linePath1 = UIBezierPath.init(ovalIn: CGRect.init(x: 0, y: 0, width: 5, height: 5))
lineShape1.path = linePath1.cgPath
lineShape1.fillColor = UIColor.init(white: 0.7, alpha: 0.5).cgColor
lineShape1.position = CGPoint(x: view.bounds.midX, y: view.bounds.midY)
view.layer.addSublayer(lineShape1)
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.