Skip to content

Instantly share code, notes, and snippets.

@charlieelliott
Last active June 26, 2017 10:19
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save charlieelliott/c2e490d119e1ecda0a83 to your computer and use it in GitHub Desktop.
Save charlieelliott/c2e490d119e1ecda0a83 to your computer and use it in GitHub Desktop.
extension CGPoint {
mutating func move(x:Int? = nil, y:Int? = nil) -> CGPoint {
if let i = x {
self.x += CGFloat(i)
}
if let i = y {
self.y += CGFloat(i)
}
return self
}
mutating func up(y:Int) -> CGPoint {
return move(y: -y)
}
mutating func down(y:Int) -> CGPoint {
return move(y: y)
}
mutating func left(x:Int) -> CGPoint {
return move(x: -x)
}
mutating func right(x:Int) -> CGPoint {
return move(x: x)
}
}
@ldrr
Copy link

ldrr commented Jun 26, 2017

I like the idea but anyway here is a bit cleaner version of it

extension CGPoint {

    mutating func move(x:CGFloat? = nil, y:CGFloat? = nil) -> CGPoint {
        if let x = x {
            self.x += x
        }

        if let y = y {
            self.y += y
        }

        return self
    }

    mutating func up(y:CGFloat) -> CGPoint {
        return move(y: -y)
    }

    mutating func down(y:CGFloat) -> CGPoint {
        return move(y: y)
    }

    mutating func left(x:CGFloat) -> CGPoint {
        return move(x: -x)
    }

    mutating func right(x:CGFloat) -> CGPoint {
        return move(x: x)
    }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment