admin管理员组文章数量:1401645
When I have
private lazy var aLine: RepairedCAShapeLayer {
let v = RepairedCAShapeLayer()
v.contentsScale = UIScreen.main.scale
v.strokeColor = cfp.color.cgColor
v.fillColor = nil
v.lineWidth = 2
v.lineJoin = .miter
return v
}
I just realized that I inevitably ...
override func layoutSubviews() {
super.layoutSubviews()
aLine.path = fancyBezDrawing().cgPath
Where fancyBezDrawing()
is a pile of code that creates a UIBezierPath.
I also just glanced in a few client code bases and the same. And innumerable examples of same on SO.
But I suppose, one could just
override func layoutSubviews() {
super.layoutSubviews()
aLine.path = fancyCGPDrawing()
Where, fancyCGPDrawing
simply creates a ... CGPath
.
Is there something I'm missing? Is there any reason one should make paths as UIBezierPath
rather than just directly as CGPath
, for layers??
When I have
private lazy var aLine: RepairedCAShapeLayer {
let v = RepairedCAShapeLayer()
v.contentsScale = UIScreen.main.scale
v.strokeColor = cfp.color.cgColor
v.fillColor = nil
v.lineWidth = 2
v.lineJoin = .miter
return v
}
I just realized that I inevitably ...
override func layoutSubviews() {
super.layoutSubviews()
aLine.path = fancyBezDrawing().cgPath
Where fancyBezDrawing()
is a pile of code that creates a UIBezierPath.
I also just glanced in a few client code bases and the same. And innumerable examples of same on SO.
But I suppose, one could just
override func layoutSubviews() {
super.layoutSubviews()
aLine.path = fancyCGPDrawing()
Where, fancyCGPDrawing
simply creates a ... CGPath
.
Is there something I'm missing? Is there any reason one should make paths as UIBezierPath
rather than just directly as CGPath
, for layers??
1 Answer
Reset to default 1I don't believe there's any major advantage in Swift to using UIBezierPath
over a CGPath
.
My assumption is that UIBezierPath
exists to make creating a CGPath
easier when writing Objective-C.
This is because the CGPath
API is a C based API and is not very discoverable - for example to move a path to a point in Objective-C to a point you'd use this global function:
void CGPathMoveToPoint(CGMutablePathRef path, const CGAffineTransform *m, CGFloat x, CGFloat y)
Where as with UIBezierPath, all the available functions are instance methods, in this case:
- (void) moveToPoint:(CGPoint) point;
This doesn't matter in Swift because the CGPath/CGMutablePath APIs have been nicely adapted into structs and instance functions.
本文标签:
版权声明:本文标题:ios - When using a CAShapeLayer, is there any advantage at all to using a UIBezierPath over just constructing a cgPath? - Stack 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744234533a2596495.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
CGPath
has features / methods which make some path construction much easier... but, sinceUIBezierPath
exposes its.cgPath
(and can be created from a cgpath) returning aCGPath
or aUIBezierPath
from a function is not a big deal. Generally speaking, as @stu said, in Swift there is very little difference in syntax.UIBezierPath
has a little overhead, and is, therefore, (in theory) slightly slower - but it's rare that would be noticeable. – DonMag Commented Mar 25 at 17:45UIBezierPath
has some advantages - for example, we can assign the.lineWidth
/.lineCapStyle
/.lineJoinStyle
to the path object and callpath.stroke()
without needing to set those properties on theCGContext
. As far as the "close the question" votes, they are flagging it as "Opinion Based" -- which would lead me to believe the voters misunderstood the question, or didn't realize that there are practical differences. – DonMag Commented Mar 25 at 20:04