替换CALayer
我按照这个答案使用创建渐变一个CALayer
。这 关键命令是
[view.layer insertSublayer:gradient atIndex:0];
我的问题是我想定期更新渐变。如果我重复调用上面的代码,那么什么也不会发生,因为新的渐变被放置在旧的渐变之下。看 这个答案。
我的问题是:如何用一个子层替换另一个子层。我应该打电话 replaceSublayer:with:
。但要做到这一点,我需要引用旧的渐变。有没有一种动态的方法来获得这个。像[view.layer getSublayerAtIndex: 0]
之类的东西?在视图控制器中存储对当前渐变的引用是最简单的方法吗?
有想法吗?
I am following this answer to create a gradient using a CALayer
. The
key command is
[view.layer insertSublayer:gradient atIndex:0];
My problem is that I would like to update the gradient regularly. If I call the above code repeatedly, then nothing happens as new gradients are placed under the old ones. See
this answer.
My question is: how does one replace a sublayer with another one. I should probably callreplaceSublayer:with:
. But to do this, I need a reference to the old gradient. Is there a dynamic way of obtaining this. Something like [view.layer getSublayerAtIndex: 0]
? Is the easiest way to just store a reference to the current gradient in the viewcontroller?
Ideas?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如果你想获得一个子层,你应该使用这个方法:
然后你可以通过替换子层
If you want to get a sublayer, you should use this method:
Then you can replace the sublayer via
除了替换图层之外,您还可以更新现有 CAGradientLayer 的
colors
、locations
和其他属性来更改显示渐变的参数。Besides replacing the layer, you can just update the existing CAGradientLayer's
colors
,locations
, and other properties to change the parameters of the displayed gradient.