Last active
December 27, 2015 17:58
-
-
Save psobko/7365774 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
//More Basic | |
CAGradientLayer *gradient = [CAGradientLayer layer]; | |
gradient.frame = self.view.bounds; | |
gradient.colors = @[(id)[[UIColor blackColor] CGColor], | |
(id)[[UIColor whiteColor] CGColor]]; | |
[self.view.layer insertSublayer:gradient atIndex:0]; | |
//More Advanced | |
+ (void)addLinearGradientToView:(UIView *)theView withColor:(UIColor *)theColor transparentToOpaque:(BOOL)transparentToOpaque | |
{ | |
CAGradientLayer *gradient = [CAGradientLayer layer]; | |
//the gradient layer must be positioned at the origin of the view | |
CGRect gradientFrame = theView.frame; | |
gradientFrame.origin.x = 0; | |
gradientFrame.origin.y = 0; | |
gradient.frame = gradientFrame; | |
//build the colors array for the gradient | |
NSArray *colors = [NSArray arrayWithObjects: | |
(id)[theColor CGColor], | |
(id)[[theColor colorWithAlphaComponent:0.9f] CGColor], | |
(id)[[theColor colorWithAlphaComponent:0.6f] CGColor], | |
(id)[[theColor colorWithAlphaComponent:0.4f] CGColor], | |
(id)[[theColor colorWithAlphaComponent:0.3f] CGColor], | |
(id)[[theColor colorWithAlphaComponent:0.1f] CGColor], | |
(id)[[UIColor clearColor] CGColor], | |
nil]; | |
//reverse the color array if needed | |
if(transparentToOpaque) | |
{ | |
colors = [[colors reverseObjectEnumerator] allObjects]; | |
} | |
//apply the colors and the gradient to the view | |
gradient.colors = colors; | |
[theView.layer insertSublayer:gradient atIndex:0]; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment