UIView 上的渐变蒙版

发布于 2024-12-06 03:22:13 字数 90 浏览 1 评论 0原文

我有一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

说不完的你爱 2024-12-13 03:22:13

是的,您可以通过设置 CAGradientLayer 作为视图的图层蒙版:

#import <QuartzCore/QuartzCore.h>

...

CAGradientLayer *maskLayer = [CAGradientLayer layer];

maskLayer.frame = view.bounds;
maskLayer.colors = @[(id)[UIColor whiteColor].CGColor, (id)[UIColor clearColor].CGColor];
maskLayer.startPoint = CGPointMake(0.5, 0);
maskLayer.endPoint = CGPointMake(0.5, 1.0);

view.layer.mask = maskLayer;

PS 您还需要将 QuartzCore.framework 链接到您的视图应用程序让一切顺利进行。

Yes, you can do that by setting CAGradientLayer as your view's layer mask:

#import <QuartzCore/QuartzCore.h>

...

CAGradientLayer *maskLayer = [CAGradientLayer layer];

maskLayer.frame = view.bounds;
maskLayer.colors = @[(id)[UIColor whiteColor].CGColor, (id)[UIColor clearColor].CGColor];
maskLayer.startPoint = CGPointMake(0.5, 0);
maskLayer.endPoint = CGPointMake(0.5, 1.0);

view.layer.mask = maskLayer;

P.S. You also need to link QuartzCore.framework to your app to make things work.

梦在深巷 2024-12-13 03:22:13

使用图像作为遮罩来防止布局问题。

尽管弗拉基米尔的答案是正确的,但问题是视图更改后同步渐变层。例如,当您旋转手机或调整视图大小时。因此,您可以使用图像代替图层:

@IBDesignable
class MaskableLabel: UILabel {
    var maskImageView = UIImageView()

    @IBInspectable
    var maskImage: UIImage? {
        didSet {
            maskImageView.image = maskImage
            updateView()
        }
    }

    override func layoutSubviews() {
        super.layoutSubviews()
        updateView()
    }

    func updateView() {
        if maskImageView.image != nil {
            maskImageView.frame = bounds
            mask = maskImageView
        }
    }
}

然后使用简单的渐变蒙版 像这样,你甚至可以在故事板中看到它。

预览

注意:

您可以使用此 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:

@IBDesignable
class MaskableLabel: UILabel {
    var maskImageView = UIImageView()

    @IBInspectable
    var maskImage: UIImage? {
        didSet {
            maskImageView.image = maskImage
            updateView()
        }
    }

    override func layoutSubviews() {
        super.layoutSubviews()
        updateView()
    }

    func updateView() {
        if maskImageView.image != nil {
            maskImageView.frame = bounds
            mask = maskImageView
        }
    }
}

Then with a simple gradient mask like this, You can see it even right in the storyboard.

Preview

Note:

You can use this class and replace UILabel with any other view you like to subclass.

???? Bones:

You can use any other shaped image as the mask just like that.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文