我画了一个矩形。当我触摸屏幕时,我可以改变宽度和高度。 默认情况下,我使x = 50,y = self.view.frame.height / 2。我需要相对于x和y拉伸矩形。如何更改 x 和 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
}
答案 0 :(得分:3)
scanRectangle.frame.origin.x += 10
scanRectangle.frame.origin.y += 10
请参阅https://developer.apple.com/documentation/coregraphics/cgrect/1454354-origin
如果你想增加矩形但是把它保持在同一位置的中心位置:
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
或
scanRectangle.frame = scanRectangle.frame.insetBy(dx: -10, dy: -10)
答案 1 :(得分:2)
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)
我认为你的意思是在某个基点上缩放矩形。
答案 2 :(得分:0)
scanRectangle.frame.origin.x += 10;
scanRectangle.frame.origin.y += 10;
是你要求的吗?