【问题标题】:Rotation Effect not working as expected inside a navigation view, but works as expected in any other view that is not in navigation view旋转效果在导航视图中未按预期工作,但在不在导航视图中的任何其他视图中按预期工作
【发布时间】:2021-09-18 10:47:30
【问题描述】:

我想添加一个形状以围绕其轴旋转并使用它来创建背景。为此,我创建了一个背景视图并决定在我的所有屏幕上使用该视图。

似乎当我在导航视图中添加背景视图时,它锚定在中心以外的其他位置。谁能告诉我如何在导航视图中实现预期结果?

但是,当我为元素提供旋转效果时,它在所有视图中都按预期工作,但包含导航视图的视图除外。

我无法在此处添加视频,但添加驱动器链接以更好地解释我的问题。

背景视图

背景视频链接(预期结果):Background View

struct BackgroundView: View {
@State private var rotateBlob : Bool = false

var body: some View {
    
    GeometryReader { bounds in
    
    ZStack {
    Image("Inner Blob 1")
    .rotationEffect(.degrees(rotateBlob ? 360 : 0),anchor: .center)
            .animation(Animation.linear(duration: 3).repeatForever(autoreverses: false))
            .onAppear(perform: {
                self.rotateBlob = true
     })
}
    .frame(width: bounds.size.width)
        
    }
}}

导航视图内的背景视图(意外结果): Background View Inside Navigation View

struct RotationBug: View {
var body: some View {
    NavigationView{
        
        VStack{
            BackgroundView()
        }
        
        .navigationTitle("Profile")
    }
}
}

【问题讨论】:

标签: ios swiftui swiftui-navigationview


【解决方案1】:
struct BackgroundView: View {
@State private var rotateDegree : CGFloat = 0

var body: some View {
    
    GeometryReader { bounds in
    
    ZStack {
    Image("Inner Blob 1")
    .rotationEffect(Angle(degrees: rotateDegree))
    .onAppear(perform: {
         withAnimation(Animation.linear(duration: 3).repeatForever(autoreverses: false)) {
             self.rotateDegree = 360
         }
     })
    .frame(width: bounds.size.width)
    }
}}

【讨论】:

    猜你喜欢
    • 2023-03-21
    • 2016-08-27
    • 1970-01-01
    • 1970-01-01
    • 2022-01-27
    • 2022-01-09
    • 1970-01-01
    • 1970-01-01
    • 2022-12-12
    相关资源
    最近更新 更多