• AnyTransition/过渡动画, MatchedGeometryEffect/匹配几何动画效果 的使用


    1. AnyTransition 过渡动画效果

      1.1 创建过度动画案例 AnyTransitionBootcamp.swift

    1. import SwiftUI
    2. /// 旋转修饰 View
    3. struct RotateViewModifier :ViewModifier{
    4. let rotation: Double
    5. func body(content: Content) -> some View {
    6. content
    7. .rotationEffect(Angle(degrees: rotation))
    8. .offset(x: rotation != 0 ? UIScreen.main.bounds.width : 0,
    9. y: rotation != 0 ? UIScreen.main.bounds.height : 0)
    10. }
    11. }
    12. // 扩展
    13. extension AnyTransition{
    14. /// 旋转
    15. static var rotation: AnyTransition{
    16. modifier(
    17. active: RotateViewModifier(rotation: 180),
    18. identity: RotateViewModifier(rotation: 0))
    19. }
    20. /// 旋转自定义角度
    21. static func rotation(rotation: Double) -> AnyTransition{
    22. modifier(
    23. active: RotateViewModifier(rotation: rotation),
    24. identity: RotateViewModifier(rotation: 0))
    25. }
    26. /// 旋转 不对称方式
    27. static var rotateOn: AnyTransition{
    28. asymmetric(
    29. insertion: .rotation,
    30. removal: .move(edge: .leading))
    31. }
    32. }
    33. /// 过渡动画
    34. struct AnyTransitionBootcamp: View {
    35. @State private var showRectangle: Bool = false
    36. var body: some View {
    37. VStack {
    38. Spacer()
    39. if showRectangle {
    40. RoundedRectangle(cornerRadius: 25)
    41. .fill(Color.black)
    42. .frame(width: 250, height: 350)
    43. .frame(maxWidth: .infinity, maxHeight: .infinity)
    44. //.transition(.move(edge: .leading))
    45. //.transition(AnyTransition.scale.animation(.easeInOut))
    46. //.transition(AnyTransition.rotation.animation(.easeInOut))
    47. // 旋转
    48. //.transition(.rotation)
    49. // 旋转自定义角度
    50. //.transition(.rotation(rotation: 1080))
    51. // 不对称旋转
    52. .transition(.rotateOn)
    53. }
    54. Spacer()
    55. Text("Click Me!")
    56. .withDefaultButtonFormatting()
    57. .padding(.horizontal, 40)
    58. .onTapGesture {
    59. //duration: 5.0
    60. withAnimation(.easeInOut) {
    61. showRectangle.toggle()
    62. }
    63. }
    64. }
    65. }
    66. }
    67. struct AnyTransitionBootcamp_Previews: PreviewProvider {
    68. static var previews: some View {
    69. AnyTransitionBootcamp()
    70. }
    71. }

      1.2 效果图:

    2. MatchedGeometryEffect 匹配几何动画效果

      2.1 创建匹配几何效果案例,MatchedGeometryEffectBootcamp.swift

    1. import SwiftUI
    2. /// 匹配几何效果
    3. struct MatchedGeometryEffectBootcamp: View {
    4. /// 是否单击
    5. @State private var isClicked: Bool = false
    6. @Namespace private var namespace
    7. var body: some View {
    8. VStack {
    9. if !isClicked {
    10. Circle()
    11. .matchedGeometryEffect(id: "rectangle", in: namespace)
    12. .frame(width: 100, height: 100)
    13. }
    14. Spacer()
    15. if isClicked {
    16. Circle()
    17. .matchedGeometryEffect(id: "rectangle", in: namespace)
    18. .frame(width: 300, height: 200)
    19. }
    20. }
    21. .frame(maxWidth: .infinity, maxHeight: .infinity)
    22. .background(Color.orange)
    23. .onTapGesture {
    24. withAnimation(.easeIn(duration: 0.5)) {
    25. isClicked.toggle()
    26. }
    27. }
    28. }
    29. }
    30. /// 匹配几何效果二
    31. struct MatchedGeometryEffectBootcamp2: View{
    32. let categories: [String] = ["Home", "Popular", "Saved"]
    33. @State private var selected: String = "Home"
    34. @Namespace private var namespace2
    35. var body: some View{
    36. HStack {
    37. ForEach(categories, id: \.self) { category in
    38. ZStack(alignment: .bottom) {
    39. if selected == category{
    40. RoundedRectangle(cornerRadius: 10)
    41. .fill(Color.red.opacity(0.5))
    42. .matchedGeometryEffect(id: "category_background", in: namespace2)
    43. .frame(width: 35, height: 2)
    44. .offset(y: 10)
    45. }
    46. Text(category)
    47. .foregroundColor(selected == category ? .red : .black)
    48. }
    49. .frame(maxWidth: .infinity)
    50. .frame(height: 55)
    51. .onTapGesture {
    52. withAnimation(.spring()) {
    53. selected = category
    54. }
    55. }
    56. }
    57. }
    58. .padding()
    59. }
    60. }
    61. struct MatchedGeometryEffectBootcamp_Previews: PreviewProvider {
    62. static var previews: some View {
    63. MatchedGeometryEffectBootcamp()
    64. //MatchedGeometryEffectBootcamp2()
    65. }
    66. }

      2.2 效果图:

         

  • 相关阅读:
    python--列表list切分(超详细)
    复习C部分:1.第一个C语言项目 2.初识数据类型 3.初识数据类型----变量和常量 4.初识变量的作用域和生命周期
    gson TypeAdapter 适配器
    Android MVVM架构 + Retrofit完成网络请求
    Chiplet:大算力的翅膀
    Go内存分配
    FastWiki一分钟本地离线部署本地企业级人工智能客服
    Win11 避坑安装WSL2 Ubuntu22.04
    k8s Pod 驱逐时间设置
    linux下python3环境中安装MySQLdb
  • 原文地址:https://blog.csdn.net/u011193452/article/details/133785249