【问题标题】:SwiftUI Card flip with two views带有两个视图的 SwiftUI 卡片翻转
【发布时间】:2020-07-03 10:12:32
【问题描述】:

我正在尝试在两个 SwiftUI 视图之间创建卡片翻转效果。单击原始视图时,它会像翻转卡片一样在 Y 轴上 3D 旋转,并且第二个视图应该在 90 度后开始可见。

使用.rotation3DEffect() 我可以轻松地旋转视图,问题是使用animation() 我不知道如何在角度达到 90 度后触发视图更改...

@State var flipped = false

 var body: some View {

    return VStack{
        Group() {
            if !self.flipped {
                MyView(color: "Blue")
            } else {
                MyView(color: "Red")
            }
        }
        .animation(.default)
        .rotation3DEffect(self.flipped ? Angle(degrees: 90): Angle(degrees: 0), axis: (x: CGFloat(0), y: CGFloat(10), z: CGFloat(0)))
        .onTapGesture {
            self.flipped.toggle()
        }

    }

如何在两个视图之间实现这样的旋转?

【问题讨论】:

    标签: ios swift swiftui


    【解决方案1】:

    简单的解决方案 通过将您的两个视图放在 ZStack 中,然后在 flipped 状态更改时显示/隐藏它们,可以使您采用的方法起作用。第二个视图的旋转需要偏移。但是这个解决方案依赖于两个视图之间的交叉淡入淡出。对于某些用例来说可能没问题。但是有一个更好的解决方案 - 虽然它有点复杂(见下文)。

    以下是一种让你的方法奏效的方法:

    struct SimpleFlipper : View {
          @State var flipped = false
    
          var body: some View {
    
                let flipDegrees = flipped ? 180.0 : 0
    
                return VStack{
                      Spacer()
    
                      ZStack() {
                            Text("Front").placedOnCard(Color.yellow).flipRotate(flipDegrees).opacity(flipped ? 0.0 : 1.0)
                            Text("Back").placedOnCard(Color.blue).flipRotate(-180 + flipDegrees).opacity(flipped ? 1.0 : 0.0)
                      }
                      .animation(.easeInOut(duration: 0.8))
                      .onTapGesture { self.flipped.toggle() }
                      Spacer()
                }
          }
    }
    
    extension View {
    
          func flipRotate(_ degrees : Double) -> some View {
                return rotation3DEffect(Angle(degrees: degrees), axis: (x: 1.0, y: 0.0, z: 0.0))
          }
    
          func placedOnCard(_ color: Color) -> some View {
                return padding(5).frame(width: 250, height: 150, alignment: .center).background(color)
          }
    }
    

    更好的解决方案 SwiftUI 有一些有用的动画工具 - 例如 GeometryEffect - 可以生成这种效果的非常流畅的版本。 SwiftUI 实验室有一些关于这个主题的优秀博客文章。具体见:https://swiftui-lab.com/swiftui-animations-part2/

    我已经简化并改编了该帖子中的一个示例,以提供卡片翻转功能。

    struct FlippingView: View {
    
          @State private var flipped = false
          @State private var animate3d = false
    
          var body: some View {
    
                return VStack {
                      Spacer()
    
                      ZStack() {
                            FrontCard().opacity(flipped ? 0.0 : 1.0)
                            BackCard().opacity(flipped ? 1.0 : 0.0)
                      }
                      .modifier(FlipEffect(flipped: $flipped, angle: animate3d ? 180 : 0, axis: (x: 1, y: 0)))
                      .onTapGesture {
                            withAnimation(Animation.linear(duration: 0.8)) {
                                  self.animate3d.toggle()
                            }
                      }
                      Spacer()
                }
          }
    }
    
    struct FlipEffect: GeometryEffect {
    
          var animatableData: Double {
                get { angle }
                set { angle = newValue }
          }
    
          @Binding var flipped: Bool
          var angle: Double
          let axis: (x: CGFloat, y: CGFloat)
    
          func effectValue(size: CGSize) -> ProjectionTransform {
    
                DispatchQueue.main.async {
                      self.flipped = self.angle >= 90 && self.angle < 270
                }
    
                let tweakedAngle = flipped ? -180 + angle : angle
                let a = CGFloat(Angle(degrees: tweakedAngle).radians)
    
                var transform3d = CATransform3DIdentity;
                transform3d.m34 = -1/max(size.width, size.height)
    
                transform3d = CATransform3DRotate(transform3d, a, axis.x, axis.y, 0)
                transform3d = CATransform3DTranslate(transform3d, -size.width/2.0, -size.height/2.0, 0)
    
                let affineTransform = ProjectionTransform(CGAffineTransform(translationX: size.width/2.0, y: size.height / 2.0))
    
                return ProjectionTransform(transform3d).concatenating(affineTransform)
          }
    }
    
    struct FrontCard : View {
          var body: some View {
                Text("One thing is for sure – a sheep is not a creature of the air.").padding(5).frame(width: 250, height: 150, alignment: .center).background(Color.yellow)
          }
    }
    
    struct BackCard : View {
          var body: some View {
                Text("If you know you have an unpleasant nature and dislike people, this is no obstacle to work.").padding(5).frame(width: 250, height: 150).background(Color.green)
          }
    }
    

    更新

    OP 询问有关在视图之外管理翻转状态的问题。这可以通过使用绑定来完成。下面是一个实现和演示的片段。 OP 还询问有无动画的翻转。这是一个改变翻转状态(这里是showBack var)是否在动画块内完成的问题。 (该片段不包含FlipEffect 结构,这与上面的代码相同。)

    struct ContentView : View {
    
          @State var showBack = false
    
          let sample1 = "If you know you have an unpleasant nature and dislike people, this is no obstacle to work."
          let sample2 = "One thing is for sure – a sheep is not a creature of the air."
    
          var body : some View {
    
                let front = CardFace(text: sample1, background: Color.yellow)
                let back = CardFace(text: sample2, background: Color.green)
                let resetBackButton = Button(action: { self.showBack = true }) { Text("Back")}.disabled(showBack == true)
                let resetFrontButton = Button(action: { self.showBack = false }) { Text("Front")}.disabled(showBack == false)
                let animatedToggle = Button(action: {
                      withAnimation(Animation.linear(duration: 0.8)) {
                            self.showBack.toggle()
                      }
                }) { Text("Toggle")}
    
    
                return
                      VStack() {
                            HStack() {
                                  resetFrontButton
                                  Spacer()
                                  animatedToggle
                                  Spacer()
                                  resetBackButton
                            }.padding()
                            Spacer()
                            FlipView(front: front, back: back, showBack: $showBack)
                            Spacer()
                }
          }
    }
    
    
    struct FlipView<SomeTypeOfViewA : View, SomeTypeOfViewB : View> : View {
    
          var front : SomeTypeOfViewA
          var back : SomeTypeOfViewB
    
          @State private var flipped = false
          @Binding var showBack : Bool
    
          var body: some View {
    
                return VStack {
                      Spacer()
    
                      ZStack() {
                            front.opacity(flipped ? 0.0 : 1.0)
                            back.opacity(flipped ? 1.0 : 0.0)
                      }
                      .modifier(FlipEffect(flipped: $flipped, angle: showBack ? 180 : 0, axis: (x: 1, y: 0)))
                      .onTapGesture {
                            withAnimation(Animation.linear(duration: 0.8)) {
                                  self.showBack.toggle()
                            }
                      }
                      Spacer()
                }
          }
    }
    
    struct CardFace<SomeTypeOfView : View> : View {
          var text : String
          var background: SomeTypeOfView
    
          var body: some View {
                Text(text)
                      .multilineTextAlignment(.center)
                      .padding(5).frame(width: 250, height: 150).background(background)
          }
    }
    

    【讨论】:

    • 谢谢,这是完美的。如果我可能会问,因为我对 SwiftUI 非常陌生,并期待学习最佳实践,那么从外部重置 FlippableView 的最简单方法是什么,无需动画且无需点击它?
    • 你需要把上面的@State private var animate3d = false改成@Binding var animate3d。这使得翻转状态的真实来源属于视图之外的东西。 (尽管如果需要,视图仍然可以通过点击手势来切换它。)当在视图之外更改值时,它会根据您是否在动画块中调用它而设置动画。
    • 我在答案的更新中提供了如何执行此操作的说明。 (顺便说一句,@Scaraux,与stackoverflow.com/questions/60806703/… 给出的(很好的)答案相比,这在将两个视图传递给视图翻转器时使用了细微的变化)
    • 感谢您提供如此详细的回答。
    猜你喜欢
    • 2017-04-03
    • 1970-01-01
    • 1970-01-01
    • 2015-10-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多