你好,我已经使用相同的微光修饰剂有一段时间了。我最近刚刚更新到新的 IOS,闪光修改器不再起作用。我确保我的代码中没有任何内容被贬值。我不确定它是否不再起作用,我认为这是由于 IOS 动画方式的新更新所致。我很欣赏一些见解。谢谢。
import SwiftUI
public struct Shimmer: ViewModifier {
let animation: Animation
@State private var phase: CGFloat = 0
public init(animation: Animation = Self.defaultAnimation) {
self.animation = animation
}
public static let defaultAnimation = Animation.linear(duration: 1.5).repeatForever(autoreverses: false)
public init(duration: Double = 1.5, bounce: Bool = false, delay: Double = 0) {
self.animation = .linear(duration: duration)
.repeatForever(autoreverses: bounce)
.delay(delay)
}
public func body(content: Content) -> some View {
content
.modifier(
AnimatedMask(phase: phase).animation(animation)
)
.onAppear { phase = 0.8 }
}
struct AnimatedMask: AnimatableModifier {
var phase: CGFloat = 0
var animatableData: CGFloat {
get { phase }
set { phase = newValue }
}
func body(content: Content) -> some View {
content
.mask(GradientMask(phase: phase).scaleEffect(4))
}
}
struct GradientMask: View {
let phase: CGFloat
let centerColor = Color.black
let edgeColor = Color.black.opacity(0.3)
@Environment(\.layoutDirection) private var layoutDirection
var body: some View {
let isRightToLeft = layoutDirection == .rightToLeft
LinearGradient(
gradient: Gradient(stops: [
.init(color: edgeColor, location: phase),
.init(color: centerColor, location: phase + 0.1),
.init(color: edgeColor, location: phase + 0.2)
]),
startPoint: isRightToLeft ? .bottomTrailing : .topLeading,
endPoint: isRightToLeft ? .topLeading : .bottomTrailing
)
}
}
}
public extension View {
@ViewBuilder func shimmering(
active: Bool = true, duration: Double = 1.5, bounce: Bool = false, delay: Double = 0
) -> some View {
if active {
self.modifier(Shimmer(duration: duration, bounce: bounce, delay: delay))
} else {
self
}
}
@ViewBuilder func shimmering(active: Bool = true, animation: Animation = Shimmer.defaultAnimation) -> some View {
if active {
self.modifier(Shimmer(animation: animation))
} else {
self
}
}
}
AnimatableModifier
已弃用。这个不需要AnimatableMask
.animation
您只需使用带有value:
参数的修改器即可为蒙版设置动画也就是说,我不确定
scaleEffect(3)
梯度逻辑为何或如何停止工作。scaleEffect(3)
只会使整个渐变变得非常大,并且您根本无法真正清楚地看到动画。如果删除scaleEffect
,动画看起来也不太好。也许梯度停止的工作方式在某个时刻发生了变化。
我认为这是您获取代码的地方。看看该存储库的历史,作者从动画停止点更改为动画渐变的起点和终点,这对我来说更有意义,而且实际上看起来相当不错。
这是代码: