将渐变应用于 CAShapeLayer
有人有将渐变应用于 CAShapeLayer 的经验吗? CAShapeLayer 是一个很棒的图层类,但它似乎只支持纯色填充着色,而我希望它具有渐变填充(实际上是一个可设置动画的渐变)。
与 CAShapeLayer 相关的所有其他内容(阴影、形状、描边颜色、可动画形状路径)都非常棒。
我尝试将 CAGradientLayer 放置在 CAShapeLayer 内,或者确实将 CAShapeLayer 设置为 GradientLayer 的遮罩并将两者添加到容器层中,但这些没有正确的结果。
我应该子类化 CAShapeLayer,还是有更好的方法?
谢谢。
Does anyone have any experience in applying a Gradient to a CAShapeLayer? CAShapeLayer is a fantastic layer class, but it appears to only support solid fill coloring, whereas I'd like it to have a gradient fill (actually an animatable gradient at that).
Everything else to do with CAShapeLayer (shadows, shapes, stroke color, animatable shape path) is fantastic.
I've tried placing a CAGradientLayer inside a CAShapeLayer, or indeed setting the CAShapeLayer as the mask of the GradientLayer and adding both to a container layer, but these don't have the right outcome.
Should I subclass CAShapeLayer, or is there a better way forward?
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以使用形状的路径创建遮罩层并将其应用到渐变层上,如下所示:
如果您还想使用阴影,则必须在渐变层下放置形状层的“副本” ,回收相同的路径引用。
You could use the path of your shape to create a masking layer and apply that on the gradient layer, like this:
If you want to also use the shadows, you would have to place a "duplicate" of the shape layer under the gradient layer, recycling the same path reference.
非常感谢@Palimondo 的出色回答!
如果有人正在寻找此解决方案的 Swift 4 + 填充动画代码:
额外。如果您还需要“填充动画”,请添加以下行:
Many thanks to @Palimondo for a great answer!
In case someone is looking for Swift 4 + filling animation code of this solution:
Extra. In case you also need a "filling animation", add this lines:
这是一个很好的解决方案,但如果您在 CAShapeLayer 上创建类别但无法立即获得视图,则可能会遇到意外问题。
请参阅 设置新创建的 CAShapeLayer 的正确框架
底线,获取路径的边界,然后使用路径边界设置渐变蒙版的框架并根据需要进行平移。这里的好处是,通过使用路径的边界而不是任何其他框架,渐变将仅适合路径边界(假设这就是您想要的)。
This is a great solution, but you might encounter unexpected problems if you're creating a category on CAShapeLayer where you don't immediately have the view.
See Setting correct frame of a newly created CAShapeLayer
Bottom line, get the bounds of the path then set the gradient mask's frame using the path bounds and translate as necessary. Good thing here is that by using the path's bounds rather than any other frame, the gradient will only fit within the path bounds (assuming that's what you want).