I have a viewController in my iOS class and in viewDidLoad, I create a gradient with CAGradient and add it to the layer of my view. I want this to appear in other views too, so what's the best way to do that? (New to objective-c and ios, I know how to do this in PHP and C#).
One way would be to make a category on CALayer, and add a method to create and add your custom gradient. Something like this:
@implementation CALayer (CustomGradient)
-(void)addCustomGradient {
CAGradientLayer *gradientLayer = [CAGradientLayer layer];
gradientLayer.frame = self.bounds;
gradientLayer.startPoint = CGPointMake(.25, 0);
gradientLayer.endPoint = CGPointMake(1, 1);
gradientLayer.colors = @[(id)[UIColor colorWithHue:.5 saturation:.6 brightness:1 alpha:1].CGColor,(id)[UIColor colorWithHue:.65 saturation:.6 brightness:1 alpha:.5].CGColor];
gradientLayer.locations = @[@0.0f,@1.00f];
[self insertSublayer:gradientLayer atIndex:0];
}
And you could use it like this:
#import "ViewController.h"
#import <QuartzCore/QuartzCore.h>
#import "CALayer+CustomGradient.h"
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
[self.view.layer addCustomGradient];
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With