【问题标题】:How can I update this custom Picker animation and transition to iOS 15?如何更新此自定义 Picker 动画并过渡到 iOS 15?
【发布时间】:2022-01-16 12:30:03
【问题描述】:

以下代码在 iOS 15 之前运行良好,但现在我收到警告 'animation' was deprecated in iOS 15.0: Use withAnimation or animation(_:value:) instead.

环顾四周,看来我应该使用.animation(_, value: someValue) - 这确实消除了警告,但是代码没有正确动画。我想这与选择器太快有关?还是影响id 视图的选择器?

这是有效的代码,但当然,我更愿意让它与 iOS 15 保持同步:


struct SamplePickerSwap: View {
    let swapAnimation: Animation = .easeInOut(duration: 0.3333)
    let swapRight: AnyTransition = .asymmetric(
        insertion: .move(edge: .trailing),
        removal: .move(edge: .leading))
    let swapLeft: AnyTransition = .asymmetric(
        insertion: .move(edge: .leading),
        removal: .move(edge: .trailing))
    
    @State private var picker = 0
    @State private var segments = ["Left", "Right"]
    var body: some View {
        
        VStack {
            Picker("", selection: $picker) {
                ForEach(0 ..< segments.count) { index in
                    Text(self.segments[index])
                        .tag(index)
                }
            }
            .pickerStyle(SegmentedPickerStyle())
            
            switch picker {
                case 0:
                    Rectangle().fill(Color.green)
                        //.animation(swapAnimation, value: page) //This does not work
                        .animation(swapAnimation) // This is deprecated...
                        .transition(swapLeft)
                case 1:
                    Rectangle().fill(Color.yellow)
                        .animation(swapAnimation)
                        .transition(swapRight)
                default:
                    Rectangle().fill(Color.green)
                        .animation(swapAnimation) //
                        .transition(swapLeft)
            }
            Spacer()
        }
        .padding()
    }
}


我尝试添加另一个 var @State private var page = 0 并使用以下内容进行更改:

            .onChange(of: picker, perform:  { _ in
                page = picker
            })

并使用 .animation(swapAnimation, value: page).animation(swapAnimation, value: picker) 的 iOS 动画代码,但似乎没有任何组合有效。

【问题讨论】:

    标签: swiftui transition deprecation-warning ios-animations


    【解决方案1】:

    我们需要在容器中添加动画(在这种情况下为VStack)以使子视图具有动画效果,例如

        VStack {
            Picker("", selection: $picker) {
                ForEach(0 ..< segments.count) { index in
                    Text(self.segments[index])
                        .tag(index)
                }
            }
            .pickerStyle(SegmentedPickerStyle())
    
            switch picker {
            case 0:
                Rectangle().fill(Color.green)
                    .transition(swapLeft)
            case 1:
                Rectangle().fill(Color.yellow)
                    .transition(swapRight)
            default:
                Rectangle().fill(Color.green)
                    .transition(swapLeft)
            }
            Spacer()
        }
        .animation(swapAnimation, value: picker) // << here !!
    

    使用 Xcode 13 / iOS 15 测试

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-03-04
      • 2012-06-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多