Created
June 9, 2013 11:02
-
-
Save PoslinskiNet/5743165 to your computer and use it in GitHub Desktop.
Simple way how to achieve Linear Gradient View in Objective-c
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
#import <UIKit/UIKit.h> | |
@interface LinearGradientView : UIView | |
@end |
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
#import "LinearGradientView.h" | |
@implementation LinearGradientView | |
- (void)drawRect:(CGRect)rect | |
{ | |
CGContextRef currentContext = UIGraphicsGetCurrentContext(); | |
CGGradientRef glossGradient; | |
CGColorSpaceRef rgbColorspace; | |
size_t num_locations = 2; | |
CGFloat locations[2] = { 0.7, 1.0 }; | |
CGFloat components[8] = { | |
0.00, 0.00, 0.00, 1, // End color | |
0.20, 0.20, 0.20, 1, // Start color | |
}; | |
rgbColorspace = CGColorSpaceCreateDeviceRGB(); | |
glossGradient = CGGradientCreateWithColorComponents(rgbColorspace, components, locations, num_locations); | |
CGRect currentBounds = self.bounds; | |
CGPoint topCenter = CGPointMake(CGRectGetMidX(currentBounds), CGRectGetMaxY(currentBounds)); | |
CGPoint minCenter = CGPointMake(CGRectGetMidX(currentBounds), CGRectGetMinY(currentBounds)); | |
CGContextDrawLinearGradient(currentContext, glossGradient, topCenter, minCenter, nil); | |
CGGradientRelease(glossGradient); | |
CGColorSpaceRelease(rgbColorspace); | |
} | |
@end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment