CAShapeLayer 位置不正确
CAShapeLayer position incorrect
我在使用 CAShapeLayer
绘制圆时遇到问题。我有一个自定义视图 MainLogoAnimationView
代码如下:
import UIKit
class MainLogoAnimationView: UIView {
@IBOutlet var customView: UIView!
var redCircle: AnimationCircleView!
// MARK: Object Lifecycle
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
setup()
}
func setup() {
_ = Bundle.main.loadNibNamed("MainLogoAnimationView", owner: self, options: nil)?[0] as! UIView
self.addSubview(customView)
customView.frame = self.bounds
let circleWidth = frame.size.width*0.25
let yPos = frame.size.height/2 - circleWidth/2
redCircle = AnimationCircleView(frame: CGRect(x: 10, y: yPos, width: circleWidth, height: circleWidth))
redCircle.backgroundColor = UIColor.yellow
addSubview(redCircle)
}
override func layoutSubviews() {
super.layoutSubviews()
}
}
在界面生成器中,我创建了一个浅蓝色 UIView
并选择了 MainLogoAnimationView
作为子视图。
AnimationCircleView
是这样的:
import UIKit
class AnimationCircleView: UIView {
// MARK: Object lifecycle
var circleColor = UIColor.red.withAlphaComponent(0.5).cgColor {
didSet {
shapeLayer.fillColor = circleColor
}
}
override init(frame: CGRect) {
super.init(frame: frame)
setup()
}
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
setup()
}
let shapeLayer = CAShapeLayer()
func setup() {
let circlePath = UIBezierPath(ovalIn: frame)
shapeLayer.path = circlePath.cgPath
shapeLayer.fillColor = circleColor
shapeLayer.frame = self.frame
shapeLayer.backgroundColor = UIColor.gray.cgColor
layer.addSublayer(shapeLayer)
}
}
截图:
似乎CAShapeLayer
框架不正确,而且红色圆圈的位置不正确。它应该在黄色方块内。我在这里做错了什么?任何指针将不胜感激!谢谢!
更新:
在你的AnimationCircleView
里面setup
改
shapeLayer.frame = self.frame
到
shapeLayer.frame = self.bounds
这应该确保在黄色矩形内绘制灰色。
编辑:
与上面的答案类似,更改
let circlePath = UIBezierPath(ovalIn: frame)
到
let circlePath = UIBezierPath(ovalIn: bounds)
将解决您的问题并在灰色区域内绘制圆圈。
CAShapeLayer
框架是相对于添加它的视图的位置计算的,因此将其设置为等于框架,将 CAShapeLayer
添加到与原点偏移的 x 和 y 位置self.frame
.
中传递的 x 和 y 的值
我在使用 CAShapeLayer
绘制圆时遇到问题。我有一个自定义视图 MainLogoAnimationView
代码如下:
import UIKit
class MainLogoAnimationView: UIView {
@IBOutlet var customView: UIView!
var redCircle: AnimationCircleView!
// MARK: Object Lifecycle
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
setup()
}
func setup() {
_ = Bundle.main.loadNibNamed("MainLogoAnimationView", owner: self, options: nil)?[0] as! UIView
self.addSubview(customView)
customView.frame = self.bounds
let circleWidth = frame.size.width*0.25
let yPos = frame.size.height/2 - circleWidth/2
redCircle = AnimationCircleView(frame: CGRect(x: 10, y: yPos, width: circleWidth, height: circleWidth))
redCircle.backgroundColor = UIColor.yellow
addSubview(redCircle)
}
override func layoutSubviews() {
super.layoutSubviews()
}
}
在界面生成器中,我创建了一个浅蓝色 UIView
并选择了 MainLogoAnimationView
作为子视图。
AnimationCircleView
是这样的:
import UIKit
class AnimationCircleView: UIView {
// MARK: Object lifecycle
var circleColor = UIColor.red.withAlphaComponent(0.5).cgColor {
didSet {
shapeLayer.fillColor = circleColor
}
}
override init(frame: CGRect) {
super.init(frame: frame)
setup()
}
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
setup()
}
let shapeLayer = CAShapeLayer()
func setup() {
let circlePath = UIBezierPath(ovalIn: frame)
shapeLayer.path = circlePath.cgPath
shapeLayer.fillColor = circleColor
shapeLayer.frame = self.frame
shapeLayer.backgroundColor = UIColor.gray.cgColor
layer.addSublayer(shapeLayer)
}
}
截图:
似乎CAShapeLayer
框架不正确,而且红色圆圈的位置不正确。它应该在黄色方块内。我在这里做错了什么?任何指针将不胜感激!谢谢!
更新:
在你的AnimationCircleView
里面setup
改
shapeLayer.frame = self.frame
到
shapeLayer.frame = self.bounds
这应该确保在黄色矩形内绘制灰色。
编辑:
与上面的答案类似,更改
let circlePath = UIBezierPath(ovalIn: frame)
到
let circlePath = UIBezierPath(ovalIn: bounds)
将解决您的问题并在灰色区域内绘制圆圈。
CAShapeLayer
框架是相对于添加它的视图的位置计算的,因此将其设置为等于框架,将 CAShapeLayer
添加到与原点偏移的 x 和 y 位置self.frame
.