I draw a rectangle. When I touch screen, I can change width and height. By default I make x = 50, y = self.view.frame.height/2. I need to stretch rectangle relative to x and y. How can I change x and y?
var scanRectangleRect = CGRect.zero
var scanRectangle: UIView = UIView()
func drawLine() {
self.view.addSubview(scanRectangle)
scanRectangle.backgroundColor = UIColor.gray
scanRectangleRect = CGRect(x: 50, y: self.view.frame.height/2, width: 400, height: 150)
scanRectangle.frame = scanRectangleRect
scanRectangle.center = CGPoint(x: scanRectangle.center.x, y: self.view.frame.height/2)
scanRectangle.isHidden = false
}
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
scanRectangle.frame.size.height += 10
scanRectangle.frame.size.width += 10
}
scanRectangle.frame.origin.x += 10
scanRectangle.frame.origin.y += 10
See https://developer.apple.com/documentation/coregraphics/cgrect/1454354-origin
If you want to increase the rect but keeping it centered on the same position:
let delta = 10
scanRectangle.frame.origin.x -= delta / 2
scanRectangle.frame.origin.y -= delta / 2
scanRectangle.frame.size.width += delta
scanRectangle.frame.size.height += delta
or
scanRectangle.frame = scanRectangle.frame.insetBy(dx: -10, dy: -10)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With