如何更改 CGRect 的 x 和 y?
How to change x and y of CGRect?
我画了一个矩形。当我触摸屏幕时,我可以改变宽度和高度。
默认情况下,我让 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
}
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)
scanRectangle.frame.origin.x += 10;
scanRectangle.frame.origin.y += 10;
这是你要的吗?
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)
我想你的意思是在某个基点上缩放矩形。
我画了一个矩形。当我触摸屏幕时,我可以改变宽度和高度。 默认情况下,我让 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
}
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)
scanRectangle.frame.origin.x += 10;
scanRectangle.frame.origin.y += 10;
这是你要的吗?
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)
我想你的意思是在某个基点上缩放矩形。