Functional approach to altering CGRect
- Alter CGRect in a more functional and declarative way
- Alter everything at once (
x
,y
,width
,height
) - Or alter by chaining individual calls for each
- Alter everything at once (
Replay the "Ugly Way" with the "Good Looking Way" ๐
var rect = someFrame
rect.origin.x += 10
rect.origin.y += 5
rect.size.width = 200
rect.size.height = 100
// Alter x, y, width, and height all at once
// Return a tuple of (x,y,width,height)
let rect = someFrame.alter { (x, y, width, height) -> Rekt in
(x+10,y+5,200,100)
}
// Chain calls for x, y, width, and height together
// Each x, y, width, and height have a function that
// takes a closure and one that takes a value
let rect = someFrame.alterX({$0+10})
.alterY({$0+5})
.alterWidth(200)
.alterHeight(100)
Carthage is a decentralized dependency manager that builds your dependencies and provides you with binary frameworks.
You can install Carthage with Homebrew using the following command:
$ brew update
$ brew install carthage
To integrate Rekt into your Xcode project using Carthage, specify it in your Cartfile
:
github "joshdholtz/Rekt-Swift" "master"
Run carthage
to build the framework and drag the built Rekt.framework
into your Xcode project.
If you prefer not to use either of the aforementioned dependency managers, you can integrate Rekt into your project manually by copying and pasting all the files in the Classes
directory.
let rect = someFrame.alter { (x, y, width, height) -> Rekt in
(x+10,y+5,200,100)
}
- Uses inout variables instead of returning tuple
- Don't need to modify all variables
let rect = someFrame.alter({ (x, y, width, height) -> Void in
x += 10
y += 5
width = 200
height = 100
return
})
let rect = someFrame.alterX({$0+10})
.alterY({$0+5})
.alterWidth({$0+200})
.alterHeight({$0+100})
let rect = someFrame.alterX(10)
.alterY(5)
.alterWidth(200)
.alterHeight(100)
Josh Holtz, [email protected], @joshdholtz
Rekt-Swift
is available under the MIT license. See the LICENSE file for more info.