AnyTransition in SwiftUI
Updated:
AnyTransition in SwiftUI
You are going to want to add some custom animations and transitions and really customize how things come on and off of the screen to really create a beautiful user experience. You can actually totally customize and create your transitions.
import SwiftUI
// MARK: - VIEW
struct AnyTransitionBootCamp: View {
// MARK: - PROPERTY
@State private var showRectangle: Bool = false
// MARK: - BODY
var body: some View {
VStack {
Spacer()
if showRectangle {
RoundedRectangle(cornerRadius: 25)
.frame(width: 250, height: 350)
.frame(maxWidth: .infinity, maxHeight: .infinity)
.transition(.rotaing(rotation: 1080))
}
Spacer()
Text("Click Me!")
.withDefaultButtonFormatting()
.padding(.horizontal, 40)
.onTapGesture {
withAnimation(.easeInOut(duration: 3.0)) {
showRectangle.toggle()
}
}
} //: VSTACK
}
}
// MARK: - VIEWMODIFIER
struct RotateViewModifier: ViewModifier {
let rotation: Double
func body(content: Content) -> some View {
content
.rotationEffect(Angle(degrees: rotation))
.offset(
x: rotation != 0 ? UIScreen.main.bounds.width : 0,
y: rotation != 0 ? UIScreen.main.bounds.height : 0)
}
}
// MARK: - EXTENSION
extension AnyTransition {
static var rotaing: AnyTransition {
return AnyTransition.modifier(
active: RotateViewModifier(rotation: 180),
identity: RotateViewModifier(rotation: 0))
}
static func rotaing(rotation: Double) -> AnyTransition {
return AnyTransition.modifier(
active: RotateViewModifier(rotation: rotation),
identity: RotateViewModifier(rotation: 0))
}
}
- AnyTransition.asymmetric (insertion, removal)
import SwiftUI
// MARK: - VIEW
struct AnyTransitionBootCamp: View {
// MARK: - PROPERTY
@State private var showRectangle: Bool = false
// MARK: - BODY
var body: some View {
VStack {
Spacer()
if showRectangle {
RoundedRectangle(cornerRadius: 25)
.frame(width: 250, height: 350)
.frame(maxWidth: .infinity, maxHeight: .infinity)
.transition(.rotateOn)
}
Spacer()
Text("Click Me!")
.withDefaultButtonFormatting()
.padding(.horizontal, 40)
.onTapGesture {
withAnimation(.easeInOut) {
showRectangle.toggle()
}
}
} //: VSTACK
}
}
// MARK: - VIEWMODIFIER
struct RotateViewModifier: ViewModifier {
let rotation: Double
func body(content: Content) -> some View {
content
.rotationEffect(Angle(degrees: rotation))
.offset(
x: rotation != 0 ? UIScreen.main.bounds.width : 0,
y: rotation != 0 ? UIScreen.main.bounds.height : 0)
}
}
// MARK: - EXTENSTION
extension AnyTransition {
static var rotaing: AnyTransition {
modifier(
active: RotateViewModifier(rotation: 180),
identity: RotateViewModifier(rotation: 0))
}
static func rotaing(rotation: Double) -> AnyTransition {
modifier(
active: RotateViewModifier(rotation: rotation),
identity: RotateViewModifier(rotation: 0))
}
static var rotateOn: AnyTransition {
asymmetric(
insertion: .rotaing,
removal: .move(edge: .leading))
}
}
🗃 Reference
SwiftUI Thinking - https://www.youtube.com/watch?v=lF6g07FDsM0&list=PLwvDm4Vfkdphc1LLLjCaEd87BEg07M97y&index=4
Leave a comment