Rekt-Swift
Functional approach to altering CGRect
Features
- 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 (
Quick Example
Replay the "Ugly Way" with the "Good Looking Way"
Ugly Way
var rect = someFrame
rect.origin.x += 10
rect.origin.y += 5
rect.size.width = 200
rect.size.height = 100
Good Looking Way
// 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
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.
Manually
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.
Usage
Alter All
let rect = someFrame.alter { (x, y, width, height) -> Rekt in
(x+10,y+5,200,100)
}
Alter All (inout)
- 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
})
Alter Individual By Chaining
let rect = someFrame.alterX({$0+10})
.alterY({$0+5})
.alterWidth({$0+200})
.alterHeight({$0+100})
Alter Individual By Value
let rect = someFrame.alterX(10)
.alterY(5)
.alterWidth(200)
.alterHeight(100)
Author
Josh Holtz, me@joshholtz.com, @joshdholtz
License
Rekt-Swift
is available under the MIT license. See the LICENSE file for more info.