Move a CGRect downwards?

Is there any way to ...

I want to add "y of 65.0" to an existing CGRect and can't seem to find any way to do it.

Sounds like a simple task to me but seems impossible to do in Xcode since "+=" isn't allowed between CGRect's.

EDIT: Tried using offsetBy and adding them using the following code. When adding the CGRect's i get the error message: No '+' candidates produce the expected contextual result type 'CGRect'

    var fromRect:CGRect = self.tableView.rectForRow(at: indexPath)
    let addRect = CGRect(x: 0.0, y: 65.0, width: 0.0, height: 0.0)
    
    //fromRect.offsetBy(dx: 0, dy: 65.0)
    fromRect = fromRect + addRect

Upvotes: -1

Views: 1480

Answers (2)

charmingToad
charmingToad

Reputation: 1597

If you are wanting to literally add an offset to a rect's position, as in your example of adding y:65, you can use rect.offsetBy(dx: 0, dy: 64).

Upvotes: 0

Stormsyders
Stormsyders

Reputation: 285

Or maybe you could just implement an operator yourself :

func +(left: CGRect, right: CGRect) -> CGRect {
    let origin = CGPoint(x: left.origin.x + right.origin.x, y: left.origin.y + right.origin.y)
    let size = CGSize(width: left.size.width + right.size.width, height: left.size.height + right.size.height)
    return CGRect(origin: origin, size: size)
}

func +=(left: inout CGRect, right: CGRect) {
    left = left + right
}

but there is a simpler solution to that problem without using operators: Just do this: (I'm not sure about this part because it seems that you want to do something more complex ?)

rect.origin.y += 65 

Upvotes: 0

Related Questions