Swiftui的旋转效果修饰符的行为与逆时针方向相比,行为不同

发布于 2025-01-20 10:48:07 字数 1812 浏览 1 评论 0原文

上下文

我有一个由 HStack 组成的子视图,其中包含(1)一条线(形状)和(2)一个文本视图。我想旋转这个子视图,就像旋转指南针一样,有一条线指向标签“N”。 rotationEffect 修饰符似乎应该为我做到这一点。然而,将旋转从 0 度切换到 90 度,然后从 90 度切换回 0 度,会产生不同的行为。

初始旋转

视图打开时线条和标签处于水平状态。将旋转角度更改为 -90 度,当线条逆时针旋转时(如预期),为线条设置动画。但是,文本字段不会与其同步旋转。相反,文本字段似乎已移出显示屏,并且仅在动画结束时重新出现。文本重新定向为垂直方向(如预期),但它不再位于行尾。

旋转回来

当旋转角度变回零时,这种意外行为就会消失。此更改会导致标签跳转到行尾(应在的位置),然后行和标签同步动画回到水平位置(如预期)。

问题

关于如何解决意外行为有什么想法吗?

代码如下所示:

import SwiftUI

// shows a line and Text that can be pivoted through 90 degrees
// with animation.
struct TestRotatingLineAndLabel: View {
    
    @State var angle = Angle.zero
    
    var body: some View {
        VStack {
            Spacer()
            
            LineAndLabel()
                .rotationEffect( angle, anchor: .bottomLeading )
                .animation(.easeInOut(duration: 1.0),
                                      value: angle)
                .padding()
            
            Spacer()
            
            Button("Change Angle") {
                angle = ( angle == Angle.zero )
                        ? Angle( degrees: -90.0 )
                        : Angle( degrees:  0.0)
            }
            Spacer()
        }
    }
}

struct LineAndLabel: View {
    var body: some View {
        HStack {
            SimpleLine()
            Text("A Label")
        }
    }
}

struct SimpleLine: Shape {
    func path(in rect: CGRect) -> Path {
            
        var path = Path()
        path.move( to: CGPoint( x: 0, y: rect.height ))
        path.addLine(to: CGPoint( x: rect.width, y: rect.height ) )
        return path
    }
}
struct TestRotatingLineAndLabel_Previews: PreviewProvider {
    static var previews: some View {
        TestMovingLabelAnimation()
    }
}

Context

I have a subView consisting of an HStack containing (1) a line (Shape) and (2) a Text View. I want to rotate this subview, exactly like a rotating compass with a line going to the label "N". The rotationEffect modifier seems like it should do that for me. However, toggling the rotation from 0 degrees to 90, then toggling it back from 90 to 0, gives distinct behaviors.

Initial Rotation

The View opens with the line and label horizontal. Changing the rotation angle to -90 degrees animates the line as it pivots counter-clockwise (as expected). However, the Text field does not rotate in sync with it. Instead, the Text field seems to move off of the display and only reappears at the end of the animation. The Text is re-oriented to the vertical (as expected) but it is no longer at the end of the line.

Rotation Back

This unexpected behavior goes away when the rotation angle changes back to zero. This change causes the label to jump to the end of line (where it ought to be) and then the line and label animate, in sync, back to horizontal (as expected).

Question

Any ideas on how to address the unexpected behavior?

The code is shown below:

import SwiftUI

// shows a line and Text that can be pivoted through 90 degrees
// with animation.
struct TestRotatingLineAndLabel: View {
    
    @State var angle = Angle.zero
    
    var body: some View {
        VStack {
            Spacer()
            
            LineAndLabel()
                .rotationEffect( angle, anchor: .bottomLeading )
                .animation(.easeInOut(duration: 1.0),
                                      value: angle)
                .padding()
            
            Spacer()
            
            Button("Change Angle") {
                angle = ( angle == Angle.zero )
                        ? Angle( degrees: -90.0 )
                        : Angle( degrees:  0.0)
            }
            Spacer()
        }
    }
}

struct LineAndLabel: View {
    var body: some View {
        HStack {
            SimpleLine()
            Text("A Label")
        }
    }
}

struct SimpleLine: Shape {
    func path(in rect: CGRect) -> Path {
            
        var path = Path()
        path.move( to: CGPoint( x: 0, y: rect.height ))
        path.addLine(to: CGPoint( x: rect.width, y: rect.height ) )
        return path
    }
}
struct TestRotatingLineAndLabel_Previews: PreviewProvider {
    static var previews: some View {
        TestMovingLabelAnimation()
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文