It doesn't work, I had already tried this.
Post
Replies
Boosts
Views
Activity
Hi,
sorry. What I wanted to say is if it is possible to use @binding properties with a Bool var because I want to press a button from one view and activate the animation in viewgraph() (which is a content inside Play).
struct Play: View {
var body: some View {
...
if Graph {
ViewGraph()
}
...
} .toolbar {
ToolbarItem(placement: .primaryAction) {
Menu{
Section {
Button(action: {
WHEN I PRESS THIS BUTTON, I WANT THE GRAPH TO START
}, label: {
Text("Start recording")
})
Button(action: {
WHEN I PRESS THIS BUTTON, I WANT THE GRAPH TO STOP
}, label: {
Text("Stop recording")
})
}
}
label: {
Label("Add", systemImage: "playpause")
}
struct ViewGraph: View {
// graph 1
struct LineGraph: Shape {
var dataPoints: [CGFloat]
func path(in rect: CGRect) -> Path {
func point(at ix: Int) -> CGPoint {
let point = dataPoints[ix]
let x = rect.width * CGFloat(ix) / CGFloat(dataPoints.count - 1)
let y = (1-point) * rect.height
return CGPoint(x: x, y: y)
}
return Path { p in
guard dataPoints.count > 1 else { return }
let start = dataPoints[0]
p.move(to: CGPoint(x: 0, y: (1-start) * rect.height))
for idx in dataPoints.indices {
p.addLine(to: point(at: idx))
}
}
}
}
// variables graph
@State public var on = false This is the var which starts the graph
let sampleDataC: [CGFloat] = [0.1, 0.2, 0.45, 0.6, -0.8, -1.1, -0.4, 0.1, 0.2, 0.45, 0.6, -0.8, -0.2, 0.5, 0.3]
@State private var ColorConG = Color.white
@State private var viewID = 0
var body: some View {
ZStack{
Image("backGraph")
.resizable()
.aspectRatio(contentMode: .fit)
VStack{
LineGraph(dataPoints: sampleDataC)
.trim(to: on ? 1 : 0)
.stroke(ColorConG, lineWidth: 1.5)
.aspectRatio(16/1, contentMode: .fit)
.frame(width: 320, height: 20, alignment: .center)
.offset(x: 5, y: 25.0)
.id(viewID)
}
Button("Animate") {
if !on{
withAnimation(.linear(duration: 130)) {
self.on.toggle()
}
}
else{
self.on.toggle()
viewID += 1
}
}
}
}
}
My idea is to create the button Animate in the other view (Play) inside the toolbar.
My idea was to use a @Binding property but I couldn't get it.