添加视图时,SwiftUI 插入转换不起作用 [英] SwiftUI insertion transion is not working when view added

查看:29
本文介绍了添加视图时,SwiftUI 插入转换不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在苹果开发者页面中尝试 SwiftUI 教程.现在我正在学习过渡教程,但在添加视图时我的过渡不起作用.

I'm trying SwiftUI tutorial in apple developer page. now I'm following transition tutorial but my transition is not working when the view added.

这是我的代码.

VStack(alignment: .leading) {
            
    HStack() {
            
        // title
        Text(titleText)
            .font(.headline)
            .padding()
            
        Spacer()
            
        // button
        Button(action: {
            withAnimation {    
                self.showDetail.toggle()
            }
        }) {
            Image(systemName: "chevron.right.circle")
                .imageScale(.large)
                .rotationEffect(.degrees(showDetail ? 90 : 0))
                .padding()
        }
    }
        
    // detail
    if showDetail {        
        Text(contentText)
            .transition(.slide)
            .padding()
    }
}

我认为具有 contentText 的文本应该与幻灯片过渡一起出现,但是当我按下按钮时,它只是弹出.但是当我再次按下按钮时,它会随着过渡而消失.所以移除过渡是可行的.

I think the Text that has contentText, should appear with slide transition but when I press the Button, it just pop up. but when I press button again it disappear with transition. so removal transition is work.

我该如何解决这个问题?

How can I fix this?

推荐答案

经过一些测试,我找到了一些解决方案.

After some test I found some solution.

解决方案1:这只是 XCode 画布的错误.它可以在模拟器或真机上运行.

solution 1: It is just bug of XCode canvas. It works in the simulator or on a real machine.

解决方案2.(如果你真的想在画布上做)

solution 2. (if you really want to do in canvas)

Button (action : {
    withAnimation(.easeInOut) {    // add animation manually
        self.showDetail.toggle()
    }
}) {
    // label Image no change
}

if showDetail {
    Text(contentText)
        .transition(.slide)
        .animation(.easeInOut)    // match with withAnimation
}

这篇关于添加视图时,SwiftUI 插入转换不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆