在收集视图中添加淡出效果

发布于 2025-02-13 01:59:51 字数 792 浏览 2 评论 0原文

如下图所示,我想在收集视图的顶部添加淡出效果。这张照片中的列表是Instagram实时广播中存在的功能。我尝试了Cagradientlayer,但没有得到我想要的结果。事先感谢您的帮助。

https://i.sstatic.net/guj7777.jpg

Instagram实时列表 希望这种效果始终保持在列表的顶部。顺便说一句,我尝试使用Cagradientlayer尝试的事情:

private func setFadeOut() {
let collectionViewGradient = CAGradientLayer()
collectionViewGradient.colors = [
    UIColor.gray.withAlphaComponent(0.2).cgColor,
    UIColor.gray.withAlphaComponent(0.1).cgColor,
    UIColor.clear.cgColor
]
collectionViewGradient.frame = view.bounds
collectionViewGradient.startPoint = CGPoint(x: 0, y: 0)
collectionViewGradient.endPoint = CGPoint(x: 0, y: 1)
collectionView.layer.insertSublayer(collectionViewGradient, at: 0)
}

I want to add a fade effect to the top of the collection view as in the photo below. The list in this photo is a feature that exists in the live broadcast of Instagram. I tried CAGradientLayer but didn't get the result I wanted. Thanks in advance for the help.

Instagram Live List: https://i.sstatic.net/gUj77.jpg

Edit: I want this effect to always stay at the top of the list. Btw, things I've tried with CAGradientLayer:

private func setFadeOut() {
let collectionViewGradient = CAGradientLayer()
collectionViewGradient.colors = [
    UIColor.gray.withAlphaComponent(0.2).cgColor,
    UIColor.gray.withAlphaComponent(0.1).cgColor,
    UIColor.clear.cgColor
]
collectionViewGradient.frame = view.bounds
collectionViewGradient.startPoint = CGPoint(x: 0, y: 0)
collectionViewGradient.endPoint = CGPoint(x: 0, y: 1)
collectionView.layer.insertSublayer(collectionViewGradient, at: 0)
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

一张白纸 2025-02-20 01:59:51

您没有正确配置梯度。您的颜色顺序不正确,您缺少位置。

尝试以下操作:

    private func setFadeOut() {
    let collectionViewGradient = CAGradientLayer()
    collectionViewGradient.colors = [
        UIColor.gray.cgColor,
        UIColor.white.cgColor,
        UIColor.white.cgColor,
        UIColor.gray.cgColor
    ]
    collectionViewGradient.locations = [0, 0.1, 0.9, 1.0]
    collectionViewGradient.frame = view.bounds
    collectionView.layer.insertSublayer(collectionViewGradient, at: 0)
}

You didn't configured the gradient properly. Your color order was incorrect and you are missing locations.

Try this:

    private func setFadeOut() {
    let collectionViewGradient = CAGradientLayer()
    collectionViewGradient.colors = [
        UIColor.gray.cgColor,
        UIColor.white.cgColor,
        UIColor.white.cgColor,
        UIColor.gray.cgColor
    ]
    collectionViewGradient.locations = [0, 0.1, 0.9, 1.0]
    collectionViewGradient.frame = view.bounds
    collectionView.layer.insertSublayer(collectionViewGradient, at: 0)
}
以歌曲疗慰 2025-02-20 01:59:51

您可以尝试使用下面的视图类。
将梯度视图放置在任何位置(顶部,底部),设置其框架,并确保Z订购还可以(如果需要,将其放在前面)。
距离参数确定点中的过渡区域。

一个示例 - 如果将其放在顶部并将其框架高度设置为150,并且距离为100,则梯度将超过100点,其余50点在顶部将是纯黑色(或您设置的任何颜色)。

通常,您要放置它,以使其在安全区域过渡,其余不安全的区域是纯黑色(凹口等)。

查看实例需要以编程方式创建,但这无论如何您似乎都在做。对于接口构建器的使用,修改它并不困难。

import UIKit

class EdgeGradientView: UIView {
    
    static override var layerClass: AnyClass {
        return CAGradientLayer.self
    }
    
    enum Placement {
        case top
        case bottom
    }
    
    private let placement: Placement
    private let distance: Int
    private let color: UIColor
    
    init(placement: Placement, distance: Int, color: UIColor) {
        self.placement = placement
        self.distance = distance
        self.color = color
        
        super.init(frame: .zero)
        
        isUserInteractionEnabled = false

        configureGradient()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override func layoutSubviews() {
        super.layoutSubviews()
        
        updateGradient()
    }

    private func configureGradient() {
        let gradientLayer = layer as! CAGradientLayer
        gradientLayer.colors = [
            color.cgColor,
            color.cgColor,
            color.withAlphaComponent(0.0).cgColor
        ]
        switch placement {
        case .top:
            gradientLayer.startPoint = CGPoint(x: 0.5, y: 0.0)
            gradientLayer.endPoint = CGPoint(x: 0.5, y: 1.0)
        case .bottom:
            gradientLayer.startPoint = CGPoint(x: 0.5, y: 1.0)
            gradientLayer.endPoint = CGPoint(x: 0.5, y: 0.0)
        }
        
        updateGradient()

    }
    
    private func updateGradient() {
        let gradientLayer = layer as! CAGradientLayer
        gradientLayer.locations = [0.0, 1.0 - Double(distance) / Double(bounds.height), 1.0] as [NSNumber]
    }
}

实例化:

let gradient = EdgeGradientView(placement: .top, distance: 100, color: .black)

You may try using a view class like the one below.
Place your gradient view wherever you want (top, bottom), set its frame and make sure the z-ordering is ok (bring it to front if needed).
The distance argument determines the transition region in points.

An example - if you place it at the top and set its frame height to be 150 and distance to be 100, the gradient will progress over 100 points and the remaining 50 points on top will be pure black (or whatever color you set it to).

Typically you want to place it so that it transitions over the safe area and the remaining unsafe area is pure black (under the notch etc).

The view instance needs to be created programmatically but this is what you seem to be doing anyway. It won't be difficult to modify it for Interface Builder use.

import UIKit

class EdgeGradientView: UIView {
    
    static override var layerClass: AnyClass {
        return CAGradientLayer.self
    }
    
    enum Placement {
        case top
        case bottom
    }
    
    private let placement: Placement
    private let distance: Int
    private let color: UIColor
    
    init(placement: Placement, distance: Int, color: UIColor) {
        self.placement = placement
        self.distance = distance
        self.color = color
        
        super.init(frame: .zero)
        
        isUserInteractionEnabled = false

        configureGradient()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override func layoutSubviews() {
        super.layoutSubviews()
        
        updateGradient()
    }

    private func configureGradient() {
        let gradientLayer = layer as! CAGradientLayer
        gradientLayer.colors = [
            color.cgColor,
            color.cgColor,
            color.withAlphaComponent(0.0).cgColor
        ]
        switch placement {
        case .top:
            gradientLayer.startPoint = CGPoint(x: 0.5, y: 0.0)
            gradientLayer.endPoint = CGPoint(x: 0.5, y: 1.0)
        case .bottom:
            gradientLayer.startPoint = CGPoint(x: 0.5, y: 1.0)
            gradientLayer.endPoint = CGPoint(x: 0.5, y: 0.0)
        }
        
        updateGradient()

    }
    
    private func updateGradient() {
        let gradientLayer = layer as! CAGradientLayer
        gradientLayer.locations = [0.0, 1.0 - Double(distance) / Double(bounds.height), 1.0] as [NSNumber]
    }
}

Instantiate with:

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