Create Gradient UIView in IOS
01. Create NSObject Class Call "GradientBackground" and add this code
.h
#import <Foundation/Foundation.h>
#import <QuartzCore/QuartzCore.h>
@interface GradientBackground : NSObject
+(CAGradientLayer*) yellowGradient;
+(CAGradientLayer*) greenGradient;
+(CAGradientLayer*) orangeGradient;
+(CAGradientLayer*) redGradient;
@end
----------------------------------------------------------------------
.m
#import "GradientBackground.h"
@implementation GradientBackground
+ (CAGradientLayer*) yellowGradient {
UIColor *darkOp =
[UIColor colorWithRed:229/255.0f green:184/255.0f blue:0/255.0f alpha:1.0];
UIColor *lightOp =
[UIColor colorWithRed:255/255.0f green:204/255.0f blue:0/255.0f alpha:1.0];
// Create
the gradient
CAGradientLayer *gradient = [CAGradientLayer layer];
// Set
colors
gradient.colors = [NSArray arrayWithObjects:
(id)lightOp.CGColor,
(id)darkOp.CGColor,
nil];
CAGradientLayer *headerLayer
= [CAGradientLayer layer];
headerLayer.colors = gradient.colors;
return headerLayer;
}
+ (CAGradientLayer*) greenGradient {
UIColor *darkOp =
[UIColor colorWithRed:86/255.0f green:178/255.0f blue:73/255.0f alpha:1.0];
UIColor *lightOp =
[UIColor colorWithRed:113/255.0f green:206/255.0f blue:101/255.0f alpha:1.0];
// Create
the gradient
CAGradientLayer *gradient = [CAGradientLayer layer];
// Set
colors
gradient.colors = [NSArray arrayWithObjects:
(id)lightOp.CGColor,
(id)darkOp.CGColor,
nil];
CAGradientLayer *headerLayer
= [CAGradientLayer layer];
headerLayer.colors = gradient.colors;
return headerLayer;
}
+ (CAGradientLayer*) orangeGradient {
//247,157,97 223,142,88
UIColor *darkOp =
[UIColor colorWithRed:223/255.0f green:142/255.0f blue:88/255.0f alpha:1.0];
UIColor *lightOp =
[UIColor colorWithRed:247/255.0f green:157/255.0f blue:97/255.0f alpha:1.0];
// Create
the gradient
CAGradientLayer *gradient = [CAGradientLayer layer];
// Set
colors
gradient.colors = [NSArray arrayWithObjects:
(id)lightOp.CGColor,
(id)darkOp.CGColor,
nil];
CAGradientLayer *headerLayer
= [CAGradientLayer layer];
headerLayer.colors = gradient.colors;
return headerLayer;
}
+ (CAGradientLayer*) redGradient {
/*
243,49,64
195,27,40
*/
UIColor *darkOp =
[UIColor colorWithRed:195/255.0f green:27/255.0f blue:40/255.0f alpha:1.0];
UIColor *lightOp =
[UIColor colorWithRed:243/255.0f green:49/255.0f blue:64/255.0f alpha:1.0];
// Create
the gradient
CAGradientLayer *gradient = [CAGradientLayer layer];
// Set
colors
gradient.colors = [NSArray arrayWithObjects:
(id)lightOp.CGColor,
(id)darkOp.CGColor,
nil];
CAGradientLayer *headerLayer
= [CAGradientLayer layer];
headerLayer.colors = gradient.colors;
return headerLayer;
}
@end
02. Add this code to your ViewController
.h
---------------------------------------------------------
#import <UIKit/UIKit.h>
@interface ViewController : UIViewController
@property(nonatomic,strong)IBOutlet UIView *adrianView;
@property(nonatomic,strong)IBOutlet UIView *adrianView1;
@property(nonatomic,strong)IBOutlet UIView *adrianView2;
@property(nonatomic,strong)IBOutlet UIView *adrianView3;
@end
.m
---------------------------------------------------
#import "ViewController.h"
#import "GradientBackground.h"
@implementation ViewController
@synthesize adrian,adrian1,adrian2,adrian3;
#pragma mark - View lifecycle
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any
additional setup after loading the view, typically from a nib.
}
- (void)viewDidUnload
{
[super viewDidUnload];
// Release
any retained subviews of the main view.
// e.g.
self.myOutlet = nil;
}
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
//Add
gradient background
CAGradientLayer *bgLayer = [BackgroundLayer orangeGradient];
bgLayer.frame = adrianView.bounds;
[adrianView.layer insertSublayer:bgLayer atIndex:0];
CAGradientLayer *bgLayer1 = [BackgroundLayer redGradient];
bgLayer1.frame = adrianView1.bounds;
[adrianView1.layer insertSublayer:bgLayer1 atIndex:0];
CAGradientLayer *bgLayer2 = [BackgroundLayer yellowGradient];
bgLayer2.frame = adrianView2.bounds;
[adrianView2.layer insertSublayer:bgLayer2 atIndex:0];
CAGradientLayer *bgLayer3 = [BackgroundLayer greenGradient];
bgLayer3.frame = adrianView3.bounds;
[adrianView3.layer insertSublayer:bgLayer3 atIndex:0];
}
- (void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];
}
- (void)viewWillDisappear:(BOOL)animated
{
[super viewWillDisappear:animated];
}
- (void)viewDidDisappear:(BOOL)animated
{
[super viewDidDisappear:animated];
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
// Return
YES for supported orientations
return
(interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
}
@end
03. Run the project
Connect the outlet in .xib file
04. Run the project
No comments:
Post a Comment