Skip to content

Instantly share code, notes, and snippets.

@xavierLowmiller
Last active June 6, 2023 03:50
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save xavierLowmiller/76625243deed678171e9a25de66fffd4 to your computer and use it in GitHub Desktop.
Save xavierLowmiller/76625243deed678171e9a25de66fffd4 to your computer and use it in GitHub Desktop.
Slide-to-Unlock animation in SwiftUI
import SwiftUI
public struct Shimmer: AnimatableModifier {
private let gradient: Gradient
@State private var position: CGFloat = 0
public var animatableData: CGFloat {
get { position }
set { position = newValue }
}
private let animation = Animation
.linear(duration: 2)
.delay(1)
.repeatForever(autoreverses: false)
init(sideColor: Color = Color(white: 0.25), middleColor: Color = .white) {
gradient = Gradient(colors: [sideColor, middleColor, sideColor])
}
public func body(content: Content) -> some View {
LinearGradient(
gradient: gradient,
startPoint: .init(x: position - 0.2 * (1 - position), y: 0.5),
endPoint: .init(x: position + 0.2 * position, y: 0.5)
)
.mask(content)
.onAppear {
withAnimation(animation) {
position = 1
}
}
}
}
// Use it like this
Text("slide to unlock")
.modifier(Shimmer())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment