Created
January 30, 2015 02:58
-
-
Save motokiee/9bddcaa04f41e1b30721 to your computer and use it in GitHub Desktop.
@IBDesignableと@IBInspectableを使ってグラデーション可能なカスタムViewを作ってみた ref: http://qiita.com/mo_to_44/items/762f42c22ae70c689cb9
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
@IBDesignable class GradationView: UIView { | |
@IBInspectable var gradationStartColor : UIColor = UIColor.whiteColor() | |
@IBInspectable var gradationEndColor : UIColor = UIColor.blackColor() | |
@IBInspectable var gradationXPosition : CGFloat = 0.5 | |
@IBInspectable var gradationYPosition : CGFloat = 0.5 | |
@IBInspectable var circleSize : CGFloat = 1.0 | |
override func drawRect(rect: CGRect) { | |
var startRed: CGFloat = 0 | |
var startGreen: CGFloat = 0 | |
var startBlue: CGFloat = 0 | |
var startAlpha: CGFloat = 0 | |
self.gradationStartColor.getRed(&startRed, green: &startGreen, blue: &startBlue, alpha: &startAlpha) | |
var endRed: CGFloat = 0 | |
var endGreen: CGFloat = 0 | |
var endBlue: CGFloat = 0 | |
var endAlpha: CGFloat = 0 | |
self.gradationEndColor.getRed(&endRed, green: &endGreen, blue: &endBlue, alpha: &endAlpha) | |
var context = UIGraphicsGetCurrentContext() | |
CGContextSaveGState(context) | |
CGContextAddEllipseInRect(context, self.frame) | |
var colorSpaceRef:CGColorSpaceRef = CGColorSpaceCreateDeviceRGB() | |
var components:[CGFloat] = [ | |
startRed, startGreen, startBlue, startAlpha, | |
endRed, endGreen, endBlue, endAlpha, | |
] | |
var locations:[CGFloat] = [ 0.0, 1.0 ] | |
let locationCount = UInt(locations.count) | |
var gradientRef: CGGradientRef = CGGradientCreateWithColorComponents(colorSpaceRef, components, locations, locationCount) | |
var frame:CGRect = self.bounds | |
var radius:CGFloat = CGRectGetWidth(frame) | |
var startCenter: CGPoint = frame.origin | |
startCenter.x += frame.size.width * self.gradationXPosition | |
startCenter.y += frame.size.height * self.gradationYPosition | |
var endCenter = startCenter | |
var startRadius: CGFloat = 0.0 | |
var endRadius : CGFloat = CGRectGetWidth(self.bounds) * self.circleSize | |
CGContextDrawRadialGradient(context, gradientRef, startCenter, startRadius, endCenter, endRadius, CGGradientDrawingOptions(kCGGradientDrawsAfterEndLocation)) | |
CGContextRestoreGState(context) | |
} | |
} |
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
func prepareForInterfaceBuilder() |
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
override func drawRect(rect: CGRect) { | |
#if TARGET_INTERFACE_BUILDER | |
// IBで作業時に描画させたいもの | |
#endif | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment