Skip to content

Instantly share code, notes, and snippets.

@guange2015
Created June 28, 2012 03:01
Show Gist options
  • Select an option

  • Save guange2015/3008574 to your computer and use it in GitHub Desktop.

Select an option

Save guange2015/3008574 to your computer and use it in GitHub Desktop.
gradient color for view.
#import "CoreText/CoreText.h"
#import <QuartzCore/QuartzCore.h>
enum GradientType {
LEFT2RIGHT = 1,
TOP2BOTTOM = 2,
RIGHT2LEFT = 3,
BOTTOM2TOP = 4
};
@interface UIView(Gradient)
@end
@implementation UIView(Gradient)
-(void)setGradientColor:(CGRect)rc startColor:(UIColor *)startColor endColor:(UIColor *)endColor type:(enum GradientType)type
{
CAGradientLayer *gradient = [CAGradientLayer layer];
gradient.frame = rc;
gradient.colors = [NSArray arrayWithObjects:(id)startColor.CGColor,
(id)endColor.CGColor, nil];
if (LEFT2RIGHT==type) {
gradient.startPoint = CGPointMake(0.0, 0.5);
gradient.endPoint = CGPointMake(1.0, 0.5);
} else if(RIGHT2LEFT==type) {
gradient.endPoint = CGPointMake(0.0, 0.5);
gradient.startPoint = CGPointMake(1.0, 0.5);
} else if(BOTTOM2TOP==type) {
gradient.endPoint = CGPointMake(0.5,0.0);
gradient.startPoint = CGPointMake(0.5,1.0);
}
[self.layer insertSublayer:gradient atIndex:0];
}
@end
// [self setGradientColor:CGRectMake(100, 100, 100, 100) startColor:[UIColor redColor] endColor:[UIColor clearColor] type:RIGHT2LEFT];
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment