TEAM LICENSES: Save money and learn new skills through a Hacking with Swift+ team license >>

SOLVED: Removal transition of rectangle is not working

Forums > SwiftUI

Hi Devs. I was studying custom transitions.

struct CornerRotateModifier: ViewModifier {
    let amount: Double;
    let anchor: UnitPoint;

    func body(content: Content) -> some View {
        content
            .rotationEffect(.degrees(amount), anchor: anchor)
            .clipped()
    }
}

extension AnyTransition {
    static func pivot(withDelay delay: Double = 0.0) -> AnyTransition {
        .asymmetric(
            insertion: .modifier(
                active: CornerRotateModifier(amount: -90, anchor: .topLeading),
                identity: CornerRotateModifier(amount: 0, anchor: .topLeading)
            )
            .animation(
                Animation.easeInOut(duration: 0.5)
                .delay(delay)
            ),
            removal: .identity
        )
    }
}

struct ContentView: View {
    @State private var isShowingRed = false

    var body: some View {
        ZStack {
            Rectangle()
                .fill(.blue)
                .frame(width: 200, height: 200)

            if isShowingRed {
                Rectangle()
                    .fill(.red)
                    .frame(width: 200, height: 200)
                    .transition(.pivot(withDelay: 1))
            }
        }
        .onTapGesture {
            withAnimation {
                isShowingRed.toggle()
            }
        }
    }
}

This code works fine if there is .identity in removal in AnyTransition extension. I want to same animation when removing element but in reverse. But when I add same modifier as above with reverse values, its not working. It is behaving wierd when double tapping. Can anyone please help me understand where am I wrong here.

Thanks.

1      

The issue arises because you're using the same anchor point (topLeading) for both insertion and removal animations. Here's why it behaves oddly:

Insertion: Rotates the red rectangle -90 degrees around the topLeading corner, making it appear from the top left. Removal: Rotates the red rectangle 90 degrees around the topLeading corner again. This essentially brings it back to its original position (0 degrees rotation) but with a slight jump due to the animation being "on top" of the previous animation. Solution:

Use different anchor points for insertion and removal to achieve the reverse animation:

Swift extension AnyTransition { static func pivot(withDelay delay: Double = 0.0) -> AnyTransition { .asymmetric( insertion: .modifier( active: CornerRotateModifier(amount: -90, anchor: .topLeading), identity: CornerRotateModifier(amount: 0, anchor: .topLeading) ) .animation( Animation.easeInOut(duration: 0.5) .delay(delay) ), removal: .modifier( active: CornerRotateModifier(amount: 90, anchor: .bottomTrailing), // Use bottomTrailing for reverse identity: CornerRotateModifier(amount: 0, anchor: .bottomTrailing) ) .animation( Animation.easeInOut(duration: 0.5) ) ) } } Use code with caution. content_copy Now, the removal animation rotates the red rectangle 90 degrees around the bottomTrailing corner, effectively reversing the insertion animation and making it disappear smoothly. And also visithacking with swift book different forum expert

1      

@davidclarks I tried the code you gave and it is now working correctly for insertion animation. but when i click again, it just disappears rather than playing exit animation. how do i make that work?

1      

Hacking with Swift is sponsored by String Catalog.

SPONSORED Get accurate app localizations in minutes using AI. Choose your languages & receive translations for 40+ markets!

Localize My App

Sponsor Hacking with Swift and reach the world's largest Swift community!

Reply to this topic…

You need to create an account or log in to reply.

All interactions here are governed by our code of conduct.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.