Shimmer
是一个超轻量的修饰器,可以为任何 SwiftUI View
添加“闪烁”效果,例如,用来显示操作正在进行中。它在浅色和深色模式、从左到右和从右到左的布局方向,以及所有 Apple 平台(iOS、macOS、tvOS、watchOS 甚至 visionOS!)上都能很好地工作。📱💻🖥️📺⌚️🥽✨
import SwiftUI
import Shimmer
⋮
⋮
Text("SwiftUI Shimmer").modifier(Shimmer())
或者更方便地
Text("SwiftUI Shimmer").shimmering()
active
:方便的参数,用于有条件地启用效果。默认为 true
。animation
:自定义动画。默认为 Shimmer.defaultAnimation
。gradient
:自定义渐变。默认为 Shimmer.defaultGradient
。bandSize
:动画蒙版的“条带”的大小。默认为 0.2 单位点,相当于渐变范围的 20%。active
:方便的参数,用于有条件地启用效果。默认为 true
。duration
:一个闪烁周期持续的时间,以秒为单位。默认值:1.5
。bounce
:是否来回反弹(反转)动画。默认为 false
。delay
:延迟的秒数。默认为 0
。您可以提供任何自定义动画
Text("Loading...")
.shimmering(
active: isAnimating,
animation: .easeInOut(duration: 2).repeatCount(5, autoreverses: false).delay(1)
)
除了原始的蒙版模式(仍然是默认模式)之外,Shimmer 现在支持其他应用渐变模式,包括作为背景或使用自定义混合模式的叠加层
Text("Custom Gradient Mode").bold()
.font(.largeTitle)
.shimmering(
gradient: Gradient(colors: [.clear, .orange, .white, .green, .clear]),
bandSize: 0.5,
mode: .overlay()
)
当然,您可以将 .shimmering(...)
与 .redacted(...)
修饰符结合使用,以创建有趣的动画骨架视图。
Text("Some text")
.redacted(reason: .placeholder)
.shimmering()
蒙版和动画现在会自动调整以适应环境的 layoutDirection
,以便更好地支持不同的语言和区域设置。
使用 package URL 或搜索 SwiftUI-Shimmer package:https://github.com/markiv/SwiftUI-Shimmer.
有关如何集成包依赖项,请参阅Adding Package Dependencies to Your App documentation。
将其添加到您的 Podfile 中
pod 'SwiftUI-Shimmer', :git => 'https://github.com/markiv/SwiftUI-Shimmer.git'
对于基于 UIKit 的较旧的闪烁效果,请参阅 UIView-Shimmer。