swift 为什么ZStack没有旋转和缩放,但是里面的内容

rvpgvaaj  于 2023-01-19  发布在  Swift
关注(0)|答案(1)|浏览(75)

我在ZStack中应用旋转手势和放大手势。但是ZStack没有旋转,但是ZStack中的图像在手势上旋转和缩放,而我在ZStack上应用了旋转和缩放。

下面是我的代码:

var body: some View {
    
    NavigationView {
        VStack {
            GeometryReader { geo in
                let w = geo.size.width
                let h = geo.size.height
                let size = getSize(h, w)
                
                if size.width > 0 && size.height > 0 {
                    
                    ZStack {
                        Image(uiImage: service.backgroundImage!)
                            .resizable()
                            .scaledToFill()
                            .frame(width: size.width, height: size.height, alignment: .center)
                            .clipped()

    //This is the ZStack --------------------

                        ZStack{
                            Image(uiImage: foregroundImage)
                                .resizable()
                                .scaledToFit()
                                .background(GeometryGetter(rect: $imageRect))
                                
                                
                        }
                        
                        .rotationEffect(rotationAngle)
                        .scaleEffect(scale)
                        .gesture(DragGesture(minimumDistance: 0 , coordinateSpace: .local)
                                    .onChanged { value in
                            self.offset.x += value.location.x - value.startLocation.x
                            self.offset.y += value.location.y - value.startLocation.y
                                        
                                        print("->> Location - \(value.location)")

                        })
                        .offset(x: offset.x, y: offset.y)
                        .gesture(simultaneous)
                        .background(.blue)
                        
                        
                    }
                    .frame(width: size.width, height: size.height, alignment: .center)
                    .clipped()
                    .frame(maxWidth: .infinity, maxHeight: .infinity)
                    .background(AppDefaults.Theme.secondaryColor)
                }
            }
            bottomContainerView()
                
        }
        
        
        .background(Color.black.ignoresSafeArea())
    }
}
nlejzf6q

nlejzf6q1#

.rotationEffect(rotationAngle)之前加上.background(.blue)
查看swiftUI中的修改器顺序问题。请查看here

相关问题