Skip to content

Instantly share code, notes, and snippets.

@PoslinskiNet
Created June 9, 2013 11:02
Show Gist options
  • Save PoslinskiNet/5743165 to your computer and use it in GitHub Desktop.
Save PoslinskiNet/5743165 to your computer and use it in GitHub Desktop.
Simple way how to achieve Linear Gradient View in Objective-c
#import <UIKit/UIKit.h>
@interface LinearGradientView : UIView
@end
#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