ios 如何真实的确定非对称过渡

qyyhg6bp  于 2023-08-08  发布在  iOS
关注(0)|答案(1)|浏览(92)

很难用语言描述我的问题。
我想做一个这样的选择器。


的数据
但是在我改变浏览方向之后,因为我已经设置了进出过渡,即使我把过渡改成新的,视图仍然使用旧的消失。(第一个和最后一个)
有什么办法可以解决这个问题吗?



下面是代码

@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)

字符串

4jb9z9bj

4jb9z9bj1#

我试着用Toggle手动控制它是哪个过渡。如果我只是在切换方向之前切换了切换,那么转换工作正常。例如向右走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)
    }
}

相关问题