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

2022-11-09移动开发问题
26

本文介绍了如何使用 UIScrollView 创建顶部淡入淡出效果?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

我的应用中有一个 UIScrollView,并且我在其他一些应用中看到,当用户滚动时,顶部会在滚动时淡出,而不仅仅是消失.

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

解决方案

简单的2020解决方案:

导入 UIKit类 FadeTail: UIIView {私有惰性 var gradientLayer: CAGradientLayer = {让 l = CAGradientLayer()l.startPoint = CGPoint(x: 0.5, y: 0)l.endPoint = CGPoint(x: 0.5, y: 1)让 baseColor = UIColor.white//例如l.colors = [baseColor.withAlphaComponent(0),baseColor.withAlphaComponent(1),].map{$0.cgColor}layer.addSublayer(l)返回 l}()覆盖 func layoutSubviews() {super.layoutSubviews()gradientLayer.frame = 边界}}

简单

  • 在情节提要中自动布局该视图,
  • 任何您想要的形状或大小
  • 在您希望淡出的视图(文本、图像、网络视图、任何内容)之上.

简单.

提示 - 渐变、圆圈等

如果您需要疯狂的圆形/带状/等渐变,请使用此处的技术:

I've got a UIScrollView in my app and I have seen in some other apps that when the user scrolls, the top section fades out on scroll rather than just dissapearing out.

I really love this effect and want to achieve it. Any ideas how it can be done?

解决方案

Simple 2020 solution:

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
    }
}

Simply

  • autolayout that view in storyboard,
  • any shape or size you wish
  • on top of the view (text, image, webview, anything) you wish to be faded.

Easy.

Tip - gradients, circles, etc

If you need crazy circular/banded/etc fades, use the techniques here: https://stackoverflow.com/a/61174086/294884

这篇关于如何使用 UIScrollView 创建顶部淡入淡出效果?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

The End
UIScrollView

相关推荐

UIScrollView 和 Cocos2D
UIScrollView and Cocos2D(UIScrollView 和 Cocos2D)...
2024-08-12 移动开发问题
2

如何在没有 UIScrollView 的情况下放大和缩小 UIImageView?
How can i zoom in and out in a UIImageView without UIScrollView?(如何在没有 UIScrollView 的情况下放大和缩小 UIImageView?)...
2024-04-15 移动开发问题
9

iPhone SDK:UIScrollView 不滚动
iPhone SDK: UIScrollView does not scroll(iPhone SDK:UIScrollView 不滚动)...
2024-04-15 移动开发问题
11

UIScrollView 分页自动布局 &故事板
UIScrollView Paging Autolayout amp; Storyboard(UIScrollView 分页自动布局 amp;故事板)...
2023-09-12 移动开发问题
7

在 UIScrollView 的底部查看,带有 AutoLayout
View at the bottom in a UIScrollView, with AutoLayout(在 UIScrollView 的底部查看,带有 AutoLayout)...
2023-09-11 移动开发问题
1

dequeueReusableCell 打破了cliptobounds
dequeueReusableCell breaks cliptobounds(dequeueReusableCell 打破了cliptobounds)...
2023-09-09 移动开发问题
1