p-x9/swift-rectangle-selector
A UI Component for selecting rectangular areas.
Demo
https://github.com/user-attachments/assets/bfb1b0ce-74d7-4037-ad4b-1fb78ec693f0
Usage
The basic usage is as follows.
let selector = RectangleSelectorView()
selector.delegate = self
// Set initial rectangle area
selector.set(
selectedFrame: .init(
origin: .init(x: 50, y: 50),
size: .init(width: 100, height: 100)
)
)
// Show selector ui for imageView
// (To select an area of the `imageView` range)
selector.show(for: imageView)
// Close
selector.dismiss()
// Selected rect
let selected: CGRect = selector.selectedRectCustom UI
Various properties can be customised by using the Config model.
var config: Config = .default
// Custom...
config.guideConfig.lineWidth = 2
config.guideConfig.lineColor = .cyan
// Apply customized config
selector.apply(config)Aspect Ratio
The aspect ratio can be fixed.
// Fixed aspect ratio (height / width)
selector.aspectMode = .fixed(0.5)
// Free
selector.aspectMode = .freeMinimum Size
It is possible to specify the minimum size of the selection area.
selector.minimumSize = .init(width: 100, height: 100)UIControl Event
Selection area change events can be received via the delegate, but also via the UIControl valueChanged event.
selector.addTarget(
self,
action: #selector(valueChanged(_:)),
for: .valueChanged
)
@objc
func valueChanged(_ sender: RectangleSelectorView) {
print("value changed", sender.selectedRect)
}License
RectangleSelector is released under the MIT License. See LICENSE
Package Metadata
Repository: p-x9/swift-rectangle-selector
Default branch: main
README: README.md