Last active
August 29, 2015 14:06
-
-
Save cruffenach/f72de7ac109081ad7fc9 to your computer and use it in GitHub Desktop.
CGRaw
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
struct CGRaw { | |
static func rawRect(#uiKitRect : CGRect) -> CGRect { | |
let scale = UIScreen.mainScreen().scale | |
return CGRect( | |
x: CGRectGetMinX(uiKitRect)*scale, | |
y: CGRectGetMinY(uiKitRect)*scale, | |
width: CGRectGetWidth(uiKitRect)*scale, | |
height: CGRectGetHeight(uiKitRect)*scale | |
) | |
} | |
static func uiKitRect(#rawRect : CGRect) -> CGRect { | |
let scale = UIScreen.mainScreen().scale | |
return CGRect( | |
x : CGRectGetMinX(rawRect)/scale, | |
y : CGRectGetMinY(rawRect)/scale, | |
width : CGRectGetWidth(rawRect)/scale, | |
height : CGRectGetHeight(rawRect)/scale | |
) | |
} | |
static func rawMainScreenScreenBounds() -> CGRect { | |
return rawRect(uiKitRect: UIScreen.mainScreen().bounds) | |
} | |
var rawRect = CGRectZero | |
var uiKitRect : CGRect { | |
get { | |
return CGRaw.uiKitRect(rawRect: self.rawRect) | |
} | |
set { | |
rawRect = CGRaw.rawRect(uiKitRect: newValue) | |
} | |
} | |
} | |
extension CGRect { | |
var rawRect : CGRect { | |
return CGRaw.rawRect(uiKitRect: self) | |
} | |
} | |
//An example view that will draw a color spectrum using scaled units instead of UIKit units | |
class ColorView : UIView { | |
override func drawRect(rect: CGRect) { | |
super.drawRect(rect) | |
let rawBounds = CGRaw.rawRect(uiKitRect: self.bounds) | |
for i in 0...Int(CGRectGetWidth(rawBounds)) { | |
for j in 0...Int(CGRectGetHeight(rawBounds)) { | |
let modi : Bool = (i % 2 == 1) | |
let modj : Bool = (j % 2 == 1) | |
let hue : CGFloat = CGFloat(i)/CGRectGetWidth(rawBounds) | |
let color = UIColor( | |
hue: CGFloat(i)/CGRectGetWidth(rawBounds), | |
saturation: CGFloat(j)/CGRectGetHeight(rawBounds), | |
brightness: CGFloat((i+j))/(CGRectGetWidth(rawBounds)+CGRectGetHeight(rawBounds)), | |
alpha: CGFloat(1.0)) | |
color.setFill() | |
UIRectFill(CGRaw.uiKitRect( | |
rawRect: CGRect( | |
x: i, | |
y: j, | |
width: 1, | |
height: 1 | |
) | |
) | |
) | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment