iPhone “slide to unlock” animation

前端 未结 13 792
感情败类
感情败类 2020-11-28 00:49

Any ideas as to how Apple implemented the \"slide to unlock\" (also, \"slide to power off\" is another identical example) animation?

I thought about some sort of ani

13条回答
  •  感情败类
    2020-11-28 01:14

    Here's a SwiftUI version:

    struct Shimmer: AnimatableModifier {
    
        private let gradient: Gradient
    
        init(sideColor: Color = Color(white: 0.25), middleColor: Color = .white) {
            gradient = Gradient(colors: [sideColor, middleColor, sideColor])
        }
    
        @State private var position: CGFloat = 0
        var animatableData: CGFloat {
            get { position }
            set { position = newValue }
        }
    
        func body(content: Content) -> some View {
            content
                .overlay(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
                                    .linear(duration: 2)
                                    .delay(1)
                                    .repeatForever(autoreverses: false)) {
                        position = 1
                    }
                }
        }
    }
    

    Use it like this:

    Text("slide to unlock")
        .modifier(Shimmer())
    

提交回复
热议问题