Gradient Layer

#import "GradientView.h" @interface GradientView () { CAGradientLayer *gradientLayer; } @end @implementation GradientView - (void)awakeFromNib { [self addGradientLayerWithColor:self.backgroundColor]; } - (void)addGradientLayerWithColor:(UIColor *)generalColor { [self addGradientLayerWithColor:generalColor secondColor:nil angle:0.125]; } - (void)addGradientLayerWithColor:(UIColor *)generalColor secondColor:(UIColor *)secondColor angle:(float)angle { if (gradientLayer) { [gradientLayer removeFromSuperlayer]; } gradientLayer = [CAGradientLayer layer]; gradientLayer.frame = self.layer.bounds; if (secondColor) { gradientLayer.colors = [NSArray arrayWithObjects: (id)generalColor.CGColor, (id)secondColor.CGColor, nil]; } else { const CGFloat* components = CGColorGetComponents(generalColor.CGColor); float red = components[0] * 255; float green = components[1] * 255; float blue = components[2] * 255; gradientLayer.colors = [NSArray arrayWithObjects: (id)[UIColor colorWithRed:red/255.f green:(green - 30)/255.f blue:(blue - 20)/255.f alpha:1.0].CGColor, (id)[UIColor colorWithRed:red/255.f green:green/255.f blue:blue/255.f alpha:1.0].CGColor, nil]; } gradientLayer.locations = [NSArray arrayWithObjects: [NSNumber numberWithFloat:0.0f], [NSNumber numberWithFloat:1.0f], nil]; [self.layer insertSublayer:gradientLayer atIndex:0]; float a = pow(sinf((2*M_PI*((angle+0.75)/2))),2); float b = pow(sinf((2*M_PI*((angle+0.0)/2))),2); float c = pow(sinf((2*M_PI*((angle+0.25)/2))),2); float d = pow(sinf((2*M_PI*((angle+0.5)/2))),2); [gradientLayer setStartPoint:CGPointMake(a, b)]; [gradientLayer setEndPoint:CGPointMake(c, d)]; self.backgroundColor = [UIColor clearColor]; self.clipsToBounds = YES; } @end
Add gradients to your views

Be the first to comment

You can use [html][/html], [css][/css], [php][/php] and more to embed the code. Urls are automatically hyperlinked. Line breaks and paragraphs are automatically generated.