iphone - 如何使用 UIScrollView 创建顶部淡入淡出效果?

标签 iphone ios uiscrollview

我的应用程序中有一个 UIScrollView,我在其他一些应用程序中看到,当用户滚动时,顶部部分会在滚动时淡出,而不是消失。

我真的很喜欢这种效果并且想要实现它。有什么想法可以做到吗?

最佳答案

简单的 2020 解决方案:

import UIKit

class FadeTail: UIIView {
    
    private lazy var gradientLayer: CAGradientLayer = {
        let l = CAGradientLayer()
        l.startPoint = CGPoint(x: 0.5, y: 0)
        l.endPoint = CGPoint(x: 0.5, y: 1)
        let baseColor = UIColor.white // for example
        l.colors = [
            baseColor.withAlphaComponent(0),
            baseColor.withAlphaComponent(1),
        ].map{$0.cgColor}
        layer.addSublayer(l)
        return l
    }()
    
    override func layoutSubviews() {
        super.layoutSubviews()
        gradientLayer.frame = bounds
    }
}

简单

  • 自动布局 Storyboard中的 View ,
  • 任何你想要的形状或尺寸
  • 在您希望淡入淡出的 View (文本、图像、 WebView 等任何内容)之上。

简单。

enter image description here

提示 - 渐变、圆圈等

如果您需要疯狂的圆形/带状/等淡入淡出,请使用此处的技术:https://stackoverflow.com/a/61174086/294884

enter image description here

关于iphone - 如何使用 UIScrollView 创建顶部淡入淡出效果?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17774761/

相关文章:

objective-c - UIScrollView 防止向一个方向滚动,而不管 contentSize/Offset

ios - 嵌套的 UIScrollView 防止弹跳冒泡并允许在列表末尾进行父分页

iphone - iPhone Appstore批准错误

iphone - UITableViewCell imageView.contentMode 在 3.0 中不起作用

iphone - 如何在 UITableView 上添加按钮而不使用自定义单元格?

ios - 在 UISegmentedControl 之间滑动以查看不同的 Collection Views

iphone - UITableView 还是 UITableViewCell?提示 "Thread 1: Program received signal: "SIGABRT”

ios - 模拟器重置问题: “data” couldn’t be removed because you don’t have permission to access it

ios - 为什么 UITableView 有效但 UIView 不

ios - 结合使用 RxSwift 为同一对象获取不同属性的 2 个 API 调用的结果