ZStack 中的旋转问题 - SwiftUI

发布于 2025-01-12 03:33:01 字数 2734 浏览 5 评论 0原文

在这里,我尝试在这两个元素中应用相同的旋转。但是这些对象或元素正在执行不需要的行为,它们的旋转方式与其他元素不同。所以我想将它们全部旋转,这样蓝卡就不会出现在图像后面。 我的意思是我希望它们都进行相同的轮换。

输入图片这里的描述

这是我的代码 -

var body: some View {
    
    VStack(spacing : 10){
        
        
        ZStack {
            ZStack (alignment : .bottom){
                ZStack(alignment : .bottom){
                    
                    Image("cat")
                                .resizable()
                                .aspectRatio(contentMode: .fill)
                                .frame(width: length, height: length)
                                
                    
                    Text("<>")
                                .frame(width: 20, height: 20)
                                .padding()
                                .background(Color.orange)
                                .cornerRadius(15)
                               
                }
                .frame(width: length, height: length)
                .background(.blue)
                .rotationEffect(Angle(degrees: Double(-totalRotation.width)))
                
                Text("<>")
                            .frame(width: 20, height: 20)
                            .padding()
                            .background(Color.orange)
                            .cornerRadius(15)
                           
                                .gesture(
                                    DragGesture()
                                        .onChanged { value in
                                            totalRotation.width = value.translation.width + currentRotation.width
                                        }
                                        .onEnded { value in
                                            currentRotation = totalRotation
                                            mainAngle = totalRotation
                                        }
                                )
                
                
                Text("\(mainAngle.width) --- \(totalRotation.width)")
                    .foregroundColor(.white)
            }
            
            .frame(width: length, height: length)
            .background(.blue)
            .rotationEffect(Angle(degrees: Double(-mainAngle.width)))
        }
        .frame(width: length, height: length)
        .rotationEffect(Angle(degrees: Double(-mainAngle.width)))
        }
    .frame(width: length, height: length)

   
    


        
        Text("\(angle)")
        
        
        
        
        }
 }[![Here the image to get much information][1]][1]

Here , I'm trying to apply same rotation in both these elements.But these object or elements are doing un wanted behaviour, they are not rotating as same as others . So I want to rotate them all as same , So that no the blue card gets behind the image.
I mean I want them all to be in same rotation.

enter image description here

Here my code -

var body: some View {
    
    VStack(spacing : 10){
        
        
        ZStack {
            ZStack (alignment : .bottom){
                ZStack(alignment : .bottom){
                    
                    Image("cat")
                                .resizable()
                                .aspectRatio(contentMode: .fill)
                                .frame(width: length, height: length)
                                
                    
                    Text("<>")
                                .frame(width: 20, height: 20)
                                .padding()
                                .background(Color.orange)
                                .cornerRadius(15)
                               
                }
                .frame(width: length, height: length)
                .background(.blue)
                .rotationEffect(Angle(degrees: Double(-totalRotation.width)))
                
                Text("<>")
                            .frame(width: 20, height: 20)
                            .padding()
                            .background(Color.orange)
                            .cornerRadius(15)
                           
                                .gesture(
                                    DragGesture()
                                        .onChanged { value in
                                            totalRotation.width = value.translation.width + currentRotation.width
                                        }
                                        .onEnded { value in
                                            currentRotation = totalRotation
                                            mainAngle = totalRotation
                                        }
                                )
                
                
                Text("\(mainAngle.width) --- \(totalRotation.width)")
                    .foregroundColor(.white)
            }
            
            .frame(width: length, height: length)
            .background(.blue)
            .rotationEffect(Angle(degrees: Double(-mainAngle.width)))
        }
        .frame(width: length, height: length)
        .rotationEffect(Angle(degrees: Double(-mainAngle.width)))
        }
    .frame(width: length, height: length)

   
    


        
        Text("\(angle)")
        
        
        
        
        }
 }[![Here the image to get much information][1]][1]

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

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

发布评论

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