animationswiftuipopoverhud

SwiftUI - Animating HUD / Pop Over like iOS Silent Mode


I'm trying to clone the following animation from iOS:

enter image description here

Here's my code. I'm stuck with the animation of the star. I'd for example like to shake it a little bit or rotate it around it's y-axis.

enter image description here

Here's my code:

struct ContentView_Previews: PreviewProvider {
    static var previews: some View {
        PopOver(image: "star.fill", color: .primary, title: "Thank You!")
    }
}

struct PopOver: View{
    
    var image: String
    var color: Color
    var title: String

    @Environment(\.colorScheme) var scheme
    
    var body: some View{
        
        HStack(spacing: 10){
            
            Image(systemName: image)
                .font(.title3)
                .foregroundColor(color)
            
            Text(title)
                .foregroundColor(.primary)
        }
        .padding(.vertical,10)
        .padding(.horizontal)
        .background(
            scheme == .dark ? Color.black : Color.white
        )
        .clipShape(Capsule())
        .shadow(color: Color.primary.opacity(0.1), radius: 5, x: 1, y: 5)
        .shadow(color: Color.primary.opacity(0.03), radius: 5, x: 0, y: -5)
        .frame(maxWidth: .infinity, maxHeight: .infinity, alignment: .top)
        .offset(y: 0)
    }
}

Can you tell me how this can be achieved?

Many Thanks!


Solution

  • you could try something like this approach:

    struct ContentView: View {
        @State var popUpPresented = false
        
        var body: some View {
            Button(action: {popUpPresented = true}) {
                Text("click me")
            }
            .popover(isPresented: $popUpPresented, arrowEdge: .top) {
                PopOver(image: "star.fill", color: .primary, title: "Thank You!")
            }
        }
    }
    
    struct PopOver: View{
        var image: String
        var color: Color
        var title: String
    
        @State var rotate = 0.0  // <-- here
        
        @Environment(\.colorScheme) var scheme
        
        var body: some View{
            HStack(spacing: 10){
                Image(systemName: image).font(.title3).foregroundColor(color)
                    // --- here ---
                    .rotation3DEffect(Angle.degrees(rotate), axis: (x: 0, y: 1, z: 0))
                    .task {
                        withAnimation(Animation.default.speed(0.3).delay(0).repeatForever(autoreverses: false)){
                            rotate = 360.0
                        }
                    }
                Text(title).foregroundColor(.primary)
            }
            .padding(.vertical,10)
            .padding(.horizontal)
            .background(scheme == .dark ? Color.black : Color.white)
            .clipShape(Capsule())
            .shadow(color: Color.primary.opacity(0.1), radius: 5, x: 1, y: 5)
            .shadow(color: Color.primary.opacity(0.03), radius: 5, x: 0, y: -5)
            .frame(maxWidth: .infinity, maxHeight: .infinity, alignment: .top)
            .offset(y: 0)
        }
    }