本文介紹了如何使用 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 創建頂部淡入淡出效果?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!
【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!