我的问题很难用语言来描述.

我想要做一个 Select 者那样的行为.

enter image description here

但在我更改浏览方向后,因为我已经设置了进入和离开过渡,即使我将过渡更改为新的过渡,视图仍然使用旧的过渡来消失.(第一张和最后一张)

有什么办法可以解决这个问题吗?

enter image description here

以下是代码

 @State var translations:[String] = ["未知"]
    @State var index = 0
    @State var transition:AnyTransition = .asymmetric(insertion: .move(edge: .trailing), removal:.move(edge: .leading)).combined(with: .opacity)
var transTexts:some View{
        HStack{
            if(translations.count > 1){
                Button {
                    if(index > 0){
                        transition = .asymmetric(insertion: .move(edge: .leading), removal:.move(edge: .trailing)).combined(with: .opacity)
                        disabled.toggle()
                        index -= 1
                    }
                } label: {
                    Image(systemName:"arrowtriangle.backward.circle.fill")
                }
                .disabled(index == 0)
                Text(translations[index])
                    .scroll()
                    .font(.body)
                    .id(index)
                    .disabled(disabled)
                    .transition(transition)
            }
            Spacer()
            if(translations.count  > 1){
                Button {
                    if(index < translations.count-1){
                        transition = .asymmetric(insertion: .move(edge: .trailing), removal:.move(edge: .leading)).combined(with: .opacity)
                        disabled.toggle()
                        index += 1
                    }
                } label: {
                    Image(systemName:"arrowtriangle.forward.circle.fill")
                }
                .disabled(index == translations.count - 1)
            }
        }
        .animation(.default,value: index)

推荐答案

我试着用Toggle手动控制它是哪个过渡.如果我只切换了before切换方向,过渡就能正常工作.例如,向右转3次,切换,然后向左转3次.

当你用按钮自动做这件事时,看起来index次更改的时间与transition次更改的时间"太接近"了,而且没有我手动更改时的效果.

如果你只是将index的更改推迟一小段时间,它应该会起作用.

transition = ...
DispatchQueue.main.asyncAfter(deadline: .now() + 0.001) {
    index += 1 // or -= 1
}

有趣的是,DispatchQueue.main.async { ... }美元并不管用.你必须给它一些时间.

我还建议使用Bool或枚举来编码它要进入的方向,而不是AnyTransition.

工作代码:

@State var translations:[String] = ["Lorem Ipsum", "Foo Bar baz", "Something Else", "AAAAA", "BBBBB"]
@State var index = 0

@State var isForward = true

var body:some View{
    VStack {
        HStack{
            if(translations.count > 1){
                Button {
                    if(index > 0){
                        isForward = false
                        DispatchQueue.main.asyncAfter(deadline: .now() + 0.001) {
                            index -= 1
                        }
                    }
                } label: {
                    Image(systemName:"arrowtriangle.backward.circle.fill")
                }
                .disabled(index == 0)
            }
            
            if translations.count > 0 {
                Text(translations[index])
                    .font(.body)
                    .id(index)
                    .transition(
                        // I did not combine with .opacity here to make it more obvious
                        isForward ?
                            .asymmetric(insertion: .move(edge: .trailing), removal:.move(edge: .leading)) :
                                .asymmetric(insertion: .move(edge: .leading), removal:.move(edge: .trailing))
                    )
            }
            
            Spacer()
            
            if(translations.count  > 1){
                Button {
                    if index < translations.count - 1 {
                        isForward = true
                        DispatchQueue.main.asyncAfter(deadline: .now() + 0.001) {
                            index += 1
                        }
                    }
                } label: {
                    Image(systemName:"arrowtriangle.forward.circle.fill")
                }
                .disabled(index == translations.count - 1)
            }
        }
        .animation(.default,value: index)
    }
}

Ios相关问答推荐

将图案UI视图动画化以模拟进度条

使用AVCaptureEventInteraction捕获音量按键快门

用于HDR显示的CI上下文选项

如何使用参与者允许并行读取,但阻止对SWIFT中资源的并发读取和写入?

SwiftUI 点击列表内不会触发 Select

@Query与SwiftData数据列表绑定

更新 flutter 和 xcode 后 Xcode 14.3 中缺少文件 (libarclite_iphoneos.a)

如何为 XCTest、SwiftUI 预览等初始化带有 @MainActor 注释的 Swift 类

更改图标会导致 SwiftUI 动画出现故障?

如何使用 AppIntents 在 SwiftUI 应用程序中打开特定视图

iOS 是否支持使用独立 .xib 文件创建的嵌套自定义子视图?

clipShape swift的三元

UIScrollview 获取touch 事件

如果已安装,则重定向到应用程序,否则重定向到 App Store

自动布局:是什么创建了名为 UIView-Encapsulated-Layout-Width & Height 的约束?

如何识别导航堆栈中的前一个视图控制器

什么是强属性属性

如何使用 presentModalViewController 创建透明视图

iOS 7 应用程序图标、启动图像和命名约定,同时保留 iOS 6 图标

类 AMSupportURLConnectionDelegate 在两者中都实现