- 1、抽象自定义动画
// 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))
}
}
- 2、使用例子
// 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
}
}
-
3、效果图
AnyTransition.gif