-
-
Save Codelaby/9170fc50d74e62f7e010cafb07835726 to your computer and use it in GitHub Desktop.
Slide-to-Unlock animation in SwiftUI
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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