From 111a2f6b8a10467b05fcf4187dc383d1fabe8c06 Mon Sep 17 00:00:00 2001 From: DreamPiggy Date: Fri, 29 May 2020 19:54:27 +0800 Subject: [PATCH 1/2] Add the convenient API support to use SwiftUI transition with ease-in-out duration --- .../Classes/Transition/Transition.swift | 45 +++++++++++++++++++ 1 file changed, 45 insertions(+) diff --git a/SDWebImageSwiftUI/Classes/Transition/Transition.swift b/SDWebImageSwiftUI/Classes/Transition/Transition.swift index ff14f49..c4a908f 100644 --- a/SDWebImageSwiftUI/Classes/Transition/Transition.swift +++ b/SDWebImageSwiftUI/Classes/Transition/Transition.swift @@ -18,6 +18,15 @@ extension AnyTransition { return AnyTransition.asymmetric(insertion: insertion, removal: removal) } + /// Fade-in transition with duration + /// - Parameter duration: transition duration, use ease-in-out + /// - Returns: A transition with duration + public static func fade(duration: Double) -> AnyTransition { + let insertion = AnyTransition.opacity.animation(.easeInOut(duration: duration)) + let removal = AnyTransition.identity + return AnyTransition.asymmetric(insertion: insertion, removal: removal) + } + /// Flip from left transition public static var flipFromLeft: AnyTransition { let insertion = AnyTransition.move(edge: .leading) @@ -25,6 +34,15 @@ extension AnyTransition { return AnyTransition.asymmetric(insertion: insertion, removal: removal) } + /// Flip from left transition with duration + /// - Parameter duration: transition duration, use ease-in-out + /// - Returns: A transition with duration + public static func flipFromLeft(duration: Double) -> AnyTransition { + let insertion = AnyTransition.move(edge: .leading).animation(.easeInOut(duration: duration)) + let removal = AnyTransition.identity + return AnyTransition.asymmetric(insertion: insertion, removal: removal) + } + /// Flip from right transition public static var flipFromRight: AnyTransition { let insertion = AnyTransition.move(edge: .trailing) @@ -32,6 +50,15 @@ extension AnyTransition { return AnyTransition.asymmetric(insertion: insertion, removal: removal) } + /// Flip from right transition with duration + /// - Parameter duration: transition duration, use ease-in-out + /// - Returns: A transition with duration + public static func flipFromRight(duration: Double) -> AnyTransition { + let insertion = AnyTransition.move(edge: .trailing).animation(.easeInOut(duration: duration)) + let removal = AnyTransition.identity + return AnyTransition.asymmetric(insertion: insertion, removal: removal) + } + /// Flip from top transition public static var flipFromTop: AnyTransition { let insertion = AnyTransition.move(edge: .top) @@ -39,10 +66,28 @@ extension AnyTransition { return AnyTransition.asymmetric(insertion: insertion, removal: removal) } + /// Flip from top transition with duration + /// - Parameter duration: transition duration, use ease-in-out + /// - Returns: A transition with duration + public static func flipFromTop(duration: Double) -> AnyTransition { + let insertion = AnyTransition.move(edge: .top).animation(.easeInOut(duration: duration)) + let removal = AnyTransition.identity + return AnyTransition.asymmetric(insertion: insertion, removal: removal) + } + /// Flip from bottom transition public static var flipFromBottom: AnyTransition { let insertion = AnyTransition.move(edge: .bottom) let removal = AnyTransition.identity return AnyTransition.asymmetric(insertion: insertion, removal: removal) } + + /// Flip from bottom transition with duration + /// - Parameter duration: transition duration, use ease-in-out + /// - Returns: A transition with duration + public static func flipFromBottom(duration: Double) -> AnyTransition { + let insertion = AnyTransition.move(edge: .bottom).animation(.easeInOut(duration: duration)) + let removal = AnyTransition.identity + return AnyTransition.asymmetric(insertion: insertion, removal: removal) + } } From fbd5a53e1f8a751056ce636a669cfb2725c677a3 Mon Sep 17 00:00:00 2001 From: DreamPiggy Date: Fri, 29 May 2020 20:10:56 +0800 Subject: [PATCH 2/2] Update the Example and Readme --- Example/SDWebImageSwiftUIDemo/ContentView.swift | 6 ++---- README.md | 3 +-- 2 files changed, 3 insertions(+), 6 deletions(-) diff --git a/Example/SDWebImageSwiftUIDemo/ContentView.swift b/Example/SDWebImageSwiftUIDemo/ContentView.swift index 345d16a..c499ba6 100644 --- a/Example/SDWebImageSwiftUIDemo/ContentView.swift +++ b/Example/SDWebImageSwiftUIDemo/ContentView.swift @@ -173,8 +173,7 @@ struct ContentView: View { WebImage(url: URL(string:url), isAnimating: self.$animated) .resizable() .indicator(.activity) - .animation(.easeInOut(duration: 0.5)) - .transition(.fade) + .transition(.fade(duration: 0.5)) .scaledToFit() .frame(width: CGFloat(100), height: CGFloat(100), alignment: .center) #endif @@ -187,8 +186,7 @@ struct ContentView: View { } */ .indicator(.activity) - .animation(.easeInOut(duration: 0.5)) - .transition(.fade) + .transition(.fade(duration: 0.5)) .scaledToFit() .frame(width: CGFloat(100), height: CGFloat(100), alignment: .center) } diff --git a/README.md b/README.md index 3377bde..b74db79 100644 --- a/README.md +++ b/README.md @@ -122,8 +122,7 @@ var body: some View { Rectangle().foregroundColor(.gray) } .indicator(.activity) // Activity Indicator - .animation(.easeInOut(duration: 0.5)) // Animation Duration - .transition(.fade) // Fade Transition + .transition(.fade(duration: 0.5)) // Fade Transition with duration .scaledToFit() .frame(width: 300, height: 300, alignment: .center) }