Skip to main content
Stack Overflow
  1. About
  2. For Teams

Return to Question

Post Timeline

Corrected ‘can’ to ‘can’t’
Source Link
LGLB
  • 23
  • 6

I need the 'size' value provided by Canvas' GraphicsContext closure, outside of the closure, but no matter what I do, whatever happens in Vegas... I mean whatever happens in Canvas, stays in Canvas - I can't mutate (?) external values no matter what I've tried. I can read/use all external values, I just cancan’t change/write to any of them. Here is a sample (and, yes, my .frame() is deciding the size in the example, but the View is destined to be a subView in other Views where the user or UI decides the actual/final size):

import SwiftUI
struct ContentView3: View {
 @State var var1 = 0.25
 @State var var2 = 0.75
 @State var someStr = "Hey"
 var someVar = 0.5
 
 var body: some View {
 
 Canvas { context, size in
 
 var1 = size.width //var1 never changes.
 var2 += 37 //var2 never changes.
 someStr = String("\(size.width)") //someStr never changes.
 //the value of someVar is allowed to pass in. 
 context.fill(Ellipse().path(in: CGRect(origin: .zero, size: CGSize(width: someVar*size.width, height: size.height))), with: .color(Color.blue))
 }
 .frame(width: 400, height: 300)
 
 //The values never changed:
 HStack {
 Spacer()
 Text("var1: \(var1)")
 Text(someStr)
 Text("var2: \(var2)")
 Spacer()
 }
 }
}
#Preview { ContentView3() }

I need the 'size' value provided by Canvas' GraphicsContext closure, outside of the closure, but no matter what I do, whatever happens in Vegas... I mean whatever happens in Canvas, stays in Canvas - I can't mutate (?) external values no matter what I've tried. I can read/use all external values, I just can change/write to any of them. Here is a sample (and, yes, my .frame() is deciding the size in the example, but the View is destined to be a subView in other Views where the user or UI decides the actual/final size):

import SwiftUI
struct ContentView3: View {
 @State var var1 = 0.25
 @State var var2 = 0.75
 @State var someStr = "Hey"
 var someVar = 0.5
 
 var body: some View {
 
 Canvas { context, size in
 
 var1 = size.width //var1 never changes.
 var2 += 37 //var2 never changes.
 someStr = String("\(size.width)") //someStr never changes.
 //the value of someVar is allowed to pass in. 
 context.fill(Ellipse().path(in: CGRect(origin: .zero, size: CGSize(width: someVar*size.width, height: size.height))), with: .color(Color.blue))
 }
 .frame(width: 400, height: 300)
 
 //The values never changed:
 HStack {
 Spacer()
 Text("var1: \(var1)")
 Text(someStr)
 Text("var2: \(var2)")
 Spacer()
 }
 }
}
#Preview { ContentView3() }

I need the 'size' value provided by Canvas' GraphicsContext closure, outside of the closure, but no matter what I do, whatever happens in Vegas... I mean whatever happens in Canvas, stays in Canvas - I can't mutate (?) external values no matter what I've tried. I can read/use all external values, I just can’t change/write to any of them. Here is a sample (and, yes, my .frame() is deciding the size in the example, but the View is destined to be a subView in other Views where the user or UI decides the actual/final size):

import SwiftUI
struct ContentView3: View {
 @State var var1 = 0.25
 @State var var2 = 0.75
 @State var someStr = "Hey"
 var someVar = 0.5
 
 var body: some View {
 
 Canvas { context, size in
 
 var1 = size.width //var1 never changes.
 var2 += 37 //var2 never changes.
 someStr = String("\(size.width)") //someStr never changes.
 //the value of someVar is allowed to pass in. 
 context.fill(Ellipse().path(in: CGRect(origin: .zero, size: CGSize(width: someVar*size.width, height: size.height))), with: .color(Color.blue))
 }
 .frame(width: 400, height: 300)
 
 //The values never changed:
 HStack {
 Spacer()
 Text("var1: \(var1)")
 Text(someStr)
 Text("var2: \(var2)")
 Spacer()
 }
 }
}
#Preview { ContentView3() }
added 158 characters in body
Source Link
LGLB
  • 23
  • 6

I need the 'size' value provided by Canvas' GraphicsContext closure, outside of the closure, but no matter what I do, whatever happens in Vegas... I mean whatever happens in Canvas, stays in Canvas - I can't mutate (?) external values no matter what I've tried. I can read/use all external values, I just can change/write to any of them. Here is a sample (and, yes, my .frame() is deciding the size in the example, but the View is destined to be a subView in other Views where the user or UI decides the actual/final size):

import SwiftUI
struct ContentView3: View {
 @State var var1 = 0.25
 @State var var2 = 0.75
 @State var someStr = "Hey"
 var someVar = 0.5
 
 var body: some View {
 
 Canvas { context, size in
 
 var1 = size.width //var1 never changes.
 var2 += 37 //var2 never changes.
 someStr = String("\(size.width)") //someStr never changes.

 //the value of someVar is allowed to pass in.  
 context.fill(Ellipse().path(in: CGRect(origin: .zero, size: CGSize(width: someVar*size.width, height: size.height))), with: .color(Color.blue))
 }
 .frame(width: 400, height: 300)
 //The values never changed:
 HStack {
 Spacer()
 Text("var1: \(var1)")
 Text(someStr)
 Text("var2: \(var2)")
 Spacer()
 }
 }
}
#Preview { ContentView3() }

I need the 'size' value provided by Canvas' GraphicsContext closure, outside of the closure, but no matter what I do, whatever happens in Vegas... I mean whatever happens in Canvas, stays in Canvas - I can't mutate (?) external values no matter what I've tried. I can read/use all external values, I just can change/write to any of them. Here is a sample (and, yes, my .frame() is deciding the size in the example, but the View is destined to be a subView in other Views where the user or UI decides the actual/final size):

import SwiftUI
struct ContentView3: View {
 @State var var1 = 0.25
 @State var var2 = 0.75
 @State var someStr = "Hey"
 var someVar = 0.5
 
 var body: some View {
 
 Canvas { context, size in
 
 var1 = size.width
 var2 += 37
 someStr = String("\(size.width)")
 
 context.fill(Ellipse().path(in: CGRect(origin: .zero, size: CGSize(width: someVar*size.width, height: size.height))), with: .color(Color.blue))
 }
 .frame(width: 400, height: 300)
 
 HStack {
 Spacer()
 Text("var1: \(var1)")
 Text(someStr)
 Text("var2: \(var2)")
 Spacer()
 }
 }
}
#Preview { ContentView3() }

I need the 'size' value provided by Canvas' GraphicsContext closure, outside of the closure, but no matter what I do, whatever happens in Vegas... I mean whatever happens in Canvas, stays in Canvas - I can't mutate (?) external values no matter what I've tried. I can read/use all external values, I just can change/write to any of them. Here is a sample (and, yes, my .frame() is deciding the size in the example, but the View is destined to be a subView in other Views where the user or UI decides the actual/final size):

import SwiftUI
struct ContentView3: View {
 @State var var1 = 0.25
 @State var var2 = 0.75
 @State var someStr = "Hey"
 var someVar = 0.5
 
 var body: some View {
 
 Canvas { context, size in
 
 var1 = size.width //var1 never changes.
 var2 += 37 //var2 never changes.
 someStr = String("\(size.width)") //someStr never changes.

 //the value of someVar is allowed to pass in.  
 context.fill(Ellipse().path(in: CGRect(origin: .zero, size: CGSize(width: someVar*size.width, height: size.height))), with: .color(Color.blue))
 }
 .frame(width: 400, height: 300)
 //The values never changed:
 HStack {
 Spacer()
 Text("var1: \(var1)")
 Text(someStr)
 Text("var2: \(var2)")
 Spacer()
 }
 }
}
#Preview { ContentView3() }
added 1 character in body
Source Link
LGLB
  • 23
  • 6

I need the 'size' value provided by Canvas' GraphicsContext closure, outside of the closure, but no matter what I do, whatever happens in Vegas... I mean whatever happens in Canvas, stays in Canvas - I can't mutate (?) external values no matter what I've tried. I can read/use all external values, I just can change/write to any of them. Here is a sample (and, yes, my .frame() is decideddeciding the size in the example, but the View is destined to be a subView in other Views where the user or UI decides the actual/final size):

import SwiftUI
struct ContentView3: View {
 @State var var1 = 0.25
 @State var var2 = 0.75
 @State var someStr = "Hey"
 var someVar = 0.5
 
 var body: some View {
 
 Canvas { context, size in
 
 var1 = size.width
 var2 += 37
 someStr = String("\(size.width)")
 
 context.fill(Ellipse().path(in: CGRect(origin: .zero, size: CGSize(width: someVar*size.width, height: size.height))), with: .color(Color.blue))
 }
 .frame(width: 400, height: 300)
 
 HStack {
 Spacer()
 Text("var1: \(var1)")
 Text(someStr)
 Text("var2: \(var2)")
 Spacer()
 }
 }
}
#Preview { ContentView3() }

I need the 'size' value provided by Canvas' GraphicsContext closure, outside of the closure, but no matter what I do, whatever happens in Vegas... I mean whatever happens in Canvas, stays in Canvas - I can't mutate (?) external values no matter what I've tried. I can read/use all external values, I just can change/write to any of them. Here is a sample (and, yes, my .frame() is decided the size in the example, but the View is destined to be a subView in other Views where the user or UI decides the actual/final size):

import SwiftUI
struct ContentView3: View {
 @State var var1 = 0.25
 @State var var2 = 0.75
 @State var someStr = "Hey"
 var someVar = 0.5
 
 var body: some View {
 
 Canvas { context, size in
 
 var1 = size.width
 var2 += 37
 someStr = String("\(size.width)")
 
 context.fill(Ellipse().path(in: CGRect(origin: .zero, size: CGSize(width: someVar*size.width, height: size.height))), with: .color(Color.blue))
 }
 .frame(width: 400, height: 300)
 
 HStack {
 Spacer()
 Text("var1: \(var1)")
 Text(someStr)
 Text("var2: \(var2)")
 Spacer()
 }
 }
}
#Preview { ContentView3() }

I need the 'size' value provided by Canvas' GraphicsContext closure, outside of the closure, but no matter what I do, whatever happens in Vegas... I mean whatever happens in Canvas, stays in Canvas - I can't mutate (?) external values no matter what I've tried. I can read/use all external values, I just can change/write to any of them. Here is a sample (and, yes, my .frame() is deciding the size in the example, but the View is destined to be a subView in other Views where the user or UI decides the actual/final size):

import SwiftUI
struct ContentView3: View {
 @State var var1 = 0.25
 @State var var2 = 0.75
 @State var someStr = "Hey"
 var someVar = 0.5
 
 var body: some View {
 
 Canvas { context, size in
 
 var1 = size.width
 var2 += 37
 someStr = String("\(size.width)")
 
 context.fill(Ellipse().path(in: CGRect(origin: .zero, size: CGSize(width: someVar*size.width, height: size.height))), with: .color(Color.blue))
 }
 .frame(width: 400, height: 300)
 
 HStack {
 Spacer()
 Text("var1: \(var1)")
 Text(someStr)
 Text("var2: \(var2)")
 Spacer()
 }
 }
}
#Preview { ContentView3() }
Source Link
LGLB
  • 23
  • 6
Loading
lang-swift

AltStyle によって変換されたページ (->オリジナル) /