Post

Replies

Boosts

Views

Activity

Using Swift UI Canvas
Hello, I am trying to use the SwiftUI canvas and I'm having a couple of issues. One is that I'd like to be able to fit a canvas view in a ScrollView, but when the canvas is inside a ScrolLView, nothing draws. Additionally, I'm trying to draw lines on the canvas on Appear, but I can't seem to get that to work either. Below is a simplified version of the code I have: struct Line: Codable, Hashable { var points: [CGPoint] var colorName: String? var rgb: [Double]? private enum CodingKeys: String, CodingKey { case points case colorName case rgb } } extension CGPoint: Hashable { public func hash(into hasher: inout Hasher) { hasher.combine(x) hasher.combine(y) } } struct CanvasExample: View { @State var lines: [Line] = [] @State var selectedColor: Color = Color.black var body: some View { Canvas { ctx, size in for line in lines { var path = Path() path.addLines(line.points) ctx.stroke(path, with: .color(line.color), style: StrokeStyle(lineWidth: 5, lineCap: .round, lineJoin: .round)) } }.gesture(DragGesture(minimumDistance: 0, coordinateSpace: .local) .onChanged({ value in let position = value.location if value.translation == .zero { lines.append(Line(points: [position], color: selectedColor)) } else { guard let lastIndex = lines.indices.last else { return } lines[lastIndex].points.append(position) } }) ) .onAppear { lines.append(Line(points: [position], color: .black)) } } } Any help would be greatly appreciated. Thank you.
4
0
916
Jan ’24
Using the SwiftUI Canvas
Hello, I was following a tutorial on how to use the SwiftUI Canvas and I can't seem to get it to work. Below is the code that I have. struct Line { var points: [CGPoint] var color: Color } struct CanvasView: View { @State var lines: [Line] = [] @State var selectedColor: Color = Color.black var body: some View { ScrollView([.horizontal, .vertical]) { Canvas { ctx, size in for line in lines { var path = Path() path.addLines(line.points) ctx.stroke(path, with: .color(line.color), style: StrokeStyle(lineWidth: 5, lineCap: .round, lineJoin: .round)) } }.gesture(DragGesture(minimumDistance: 0, coordinateSpace: .local) .onChanged({ value in let position = value.location if value.translation == .zero { lines.append(Line(points: [position], color: selectedColor)) } else { guard let lastIndex = lines.indices.last else { return } lines[lastIndex].points.append(position) } }) ) } } } I'm pretty sure I followed the tutorial exactly so I'm not sure why it isn't drawing. Any help would be greatly appreciated. Thank you.
3
0
1.5k
Jan ’24
Cannot convert value of type '() -> ()' to expected argument type 'String' SwiftUI Button
Hello, I'm trying to build a simple button. For some reason, I'm getting the following errors: Cannot convert value of type '() -> ()' to expected argument type 'String' Incorrect argument label in call (have 'action:_:', expected 'value:selected:') Button(action: login) { ZStack { RoundedRectangle(cornerRadius: 5) .fill(Color.black) .frame(width: UIScreen.main.bounds.width - 50, height: 50) Text("Sign in") .foregroundColor(.white) } } All of my buttons in this particular project suddenly have this error. Any help would be greatly appreciated. Thank you
3
0
889
Dec ’23
Previewing view with Binding variable
Hello, I'm having trouble previewing a view that has a binding variable. When it's in the below state, I get the error "Edit placeholder in source file": static var previews: some View { Navigation(viewState: Binding<String>) } } When I put a string as a placeholder, I get the error: Cannot convert value of type 'String' to expected argument type 'Binding' static var previews: some View { Navigation(viewState: "sign-in") } } The code for the supporting view with the binding variable is below: struct Navigation: View { @Binding var viewState: String var body: some View { ZStack { HStack { Spacer() Button(action: { toggleView(view: "savings")}) { Text("Savings") } Spacer() Button(action: { toggleView(view: "settings")}) { Text("Settings") } Spacer() } } .position(x: UIScreen.main.bounds.width / 2, y: UIScreen.main.bounds.height / 2) .frame(width: UIScreen.main.bounds.width, height: 100) } func toggleView(view: String) { viewState = view } } Any help would be greatly appreciated.
1
0
878
Dec ’23