UIView 上的渐变蒙版
我有一个 UIView 并希望其底部淡出至 0 不透明度。
可以对 UIView (CALayer) 执行此操作以影响整个 UIView 及其内容吗?
I have a UIView and want to have the bottom part of it fade out to 0 opacity.
May this be done to a UIView (CALayer) in order to affect an entire UIView and its content?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
是的,您可以通过设置
CAGradientLayer
作为视图的图层蒙版:PS 您还需要将
QuartzCore.framework
链接到您的视图应用程序让一切顺利进行。Yes, you can do that by setting
CAGradientLayer
as your view's layer mask:P.S. You also need to link
QuartzCore.framework
to your app to make things work.使用图像作为遮罩来防止布局问题。
尽管弗拉基米尔的答案是正确的,但问题是视图更改后同步渐变层。例如,当您旋转手机或调整视图大小时。因此,您可以使用图像代替图层:
然后使用简单的渐变蒙版 像这样,你甚至可以在故事板中看到它。
注意:
您可以使用此
class
并将UILabel
替换为您想要子类化的任何其他视图。Using an Image as the mask to prevent Layout issues.
Although Vladimir's answer is correct, the issue is to sync the gradient layer after view changes. For example when you rotate the phone or resizing the view. So instead of a layer, you can use an image for that:
Then with a simple gradient mask like this, You can see it even right in the storyboard.
Note:
You can use this
class
and replaceUILabel
with any other view you like to subclass.???? Bones:
You can use any other shaped image as the mask just like that.