仅在图表框架中捏缩放图表

时间:2019-10-22 20:22:59

标签: swiftui

我想使自定义线性图图表可缩放(?)。经过研究,我用捏来缩放(swiftUI中的MagnificationGesture)来制作它。现在我的图形可以放大或缩小。但是当我缩小图形时,它覆盖了我永远都不需要的整个场景。我希望我的图形仅在图表框架中缩小(我的意思是屏幕的有限区域)。我怎么解决这个问题?

@State var scale:CGFloat = 1.0

let frame = CGSize(width: 350, height: 500)

public init(data: [Int], style: ChartStyle = Styles.lineChartStyleOne ){
    self.style = style
}

var body: some View {
    ZStack(alignment: .center){
        VStack(alignment: .leading){
            Spacer()
            GeometryReader{ geometry in

                Line(data: self.data, frame: .constant(geometry.frame(in: .local)), touchLocation: self.$touchLocation, showIndicator: self.$showIndicatorDot)
                    .offset(x: 15,y:0)

                Legend(data: self.data, frame: .constant(geometry.frame(in: .local)), hideHorizontalLines: .constant(false))

                RangeLineView(data: self.data, frame: .constant(geometry.frame(in: .local)))
                RangeView(data: self.data, frame: .constant(geometry.frame(in: .local)))
            }
            .frame(width: frame.width, height: frame.height)
                .scaleEffect(scale)
                .gesture(MagnificationGesture()
                .onChanged { value in
                    self.scale = value.magnitude
                }

                  )

            .offset(x: 0, y: 0)

        }.frame(width: self.style.chartFormSize.width, height: self.style.chartFormSize.height)
    }
}

this is what happen after zoom out. graph cover whole scene. I want it zoom out and show in only original graph frame box

1 个答案:

答案 0 :(得分:0)

想像一下我的自我...

.clipped(Rectangle())

很简单,但是它将起到神奇的作用。