In Swift, you can create beautiful drawings using the Canvas and Shapes features provided by the SwiftUI framework. Here’s a simple example of how to draw various shapes like rectangles, circles, and lines within a Canvas view.
// Example of drawing shapes with SwiftUI
import SwiftUI
struct ContentView: View {
var body: some View {
Canvas { context, size in
// Draw a rectangle
context.fill(Rectangle().path(in: CGRect(x: 20, y: 20, width: 100, height: 100)), with: .color(.blue))
// Draw a circle
context.fill(Circle().path(in: CGRect(x: 150, y: 20, width: 100, height: 100)), with: .color(.red))
// Draw a line
context.stroke(Line(from: CGPoint(x: 20, y: 150), to: CGPoint(x: 200, y: 150)), with: .color(.green), lineWidth: 5)
}
.frame(width: 300, height: 300)
}
}
struct Line: Shape {
var from: CGPoint
var to: CGPoint
func path(in rect: CGRect) -> Path {
var path = Path()
path.move(to: from)
path.addLine(to: to)
return path
}
}
// Don't forget to set ContentView as the main view in your App struct.
How do I avoid rehashing overhead with std::set in multithreaded code?
How do I find elements with custom comparators with std::set for embedded targets?
How do I erase elements while iterating with std::set for embedded targets?
How do I provide stable iteration order with std::unordered_map for large datasets?
How do I reserve capacity ahead of time with std::unordered_map for large datasets?
How do I erase elements while iterating with std::unordered_map in multithreaded code?
How do I provide stable iteration order with std::map for embedded targets?
How do I provide stable iteration order with std::map in multithreaded code?
How do I avoid rehashing overhead with std::map in performance-sensitive code?
How do I merge two containers efficiently with std::map for embedded targets?