3

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
}
rmaddy
  • 314,917
  • 42
  • 532
  • 579
kvv
  • 85
  • 2
  • 8

3 Answers3

3
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)
Sulthan
  • 128,090
  • 22
  • 218
  • 270
3
let distance: CGFloat = 10
scanRectangle.frame = CGRect(x: scanRectangle.frame.origin.x + distance, 
                             y: scanRectangle.frame.origin.y + distance, 
                         width: scanRectangle.frame.size.width - distance, 
                        height: scanRectangle.frame.size.height - distance)

I think what you mean is to scale the rectangle upon some base point.

antonio081014
  • 3,553
  • 1
  • 30
  • 37
0
scanRectangle.frame.origin.x += 10;
scanRectangle.frame.origin.y += 10;

is that what you are asking for?

wheeler
  • 647
  • 8
  • 17