mad_mask
mad_mask

Reputation: 786

Changing Gradient Background layer color for UiView IOS using CAGradientLayer classe

i want to change the color of my gradient background when i trigger an action. I tryed by many way with no succes.

BackgroundLayer.h

    #import <Foundation/Foundation.h>
    #import <QuartzCore/QuartzCore.h>

    @interface BackgroundLayer : NSObject

    +(CAGradientLayer*) greyGradient;
    +(CAGradientLayer*) blueGradient;

    @end

BackgroundLayer.m

@implementation BackgroundLayer

//Metallic grey gradient background
+ (CAGradientLayer*) greyGradient {

    UIColor *colorOne = [UIColor colorWithWhite:0.9 alpha:1.0];
    UIColor *colorTwo = [UIColor colorWithHue:0.625 saturation:0.0 brightness:0.85 alpha:1.0];
    UIColor *colorThree     = [UIColor colorWithHue:0.625 saturation:0.0 brightness:0.7 alpha:1.0];
    UIColor *colorFour = [UIColor colorWithHue:0.625 saturation:0.0 brightness:0.4 alpha:1.0];

    NSArray *colors =  [NSArray arrayWithObjects:(id)colorOne.CGColor, colorTwo.CGColor, colorThree.CGColor, colorFour.CGColor, nil];

    NSNumber *stopOne = [NSNumber numberWithFloat:0.0];
    NSNumber *stopTwo = [NSNumber numberWithFloat:0.02];
    NSNumber *stopThree     = [NSNumber numberWithFloat:0.99];
    NSNumber *stopFour = [NSNumber numberWithFloat:1.0];

    NSArray *locations = [NSArray arrayWithObjects:stopOne, stopTwo, stopThree, stopFour, nil];
    CAGradientLayer *headerLayer = [CAGradientLayer layer];
    headerLayer.colors = colors;
    headerLayer.locations = locations;

    return headerLayer;

}

//Blue gradient background
+ (CAGradientLayer*) blueGradient {

    UIColor *colorOne = [UIColor colorWithRed:(120/255.0) green:(135/255.0) blue:(150/255.0) alpha:1.0];
    UIColor *colorTwo = [UIColor colorWithRed:(57/255.0)  green:(79/255.0)  blue:(96/255.0)  alpha:1.0];

    NSArray *colors = [NSArray arrayWithObjects:(id)colorOne.CGColor, colorTwo.CGColor, nil];
    NSNumber *stopOne = [NSNumber numberWithFloat:0.0];
    NSNumber *stopTwo = [NSNumber numberWithFloat:1.0];

    NSArray *locations = [NSArray arrayWithObjects:stopOne, stopTwo, nil];

    CAGradientLayer *headerLayer = [CAGradientLayer layer];
    headerLayer.colors = colors;
    headerLayer.locations = locations;

    return headerLayer;

}

@end

In my view controller i have a switch on proximity (i work with ibeacon). the switch have 4 cases. i want Case 1(Far) with blue gradient. Case 2(Near) with grey gradient. I initialise with the layer in "(void)viewWillAppear:(BOOL)animated" and then i tryed that in the did range delegate :

case CLProximityFar:
{

[[[self.view.layer sublayers] objectAtIndex:0] removeFromSuperlayer];
self.bgLayer = [BackgroundLayer blueGradient];
self.bgLayer.frame = self.view.bounds;
[self.view.layer insertSublayer:self.bgLayer atIndex:0];
....

not working. i also tryed like this

case CLProximityNear:
{

[[[self.view.layer sublayers] objectAtIndex:0] removeFromSuperlayer];
CAGradientLayer *gradient = [[self.view.layer sublayers] firstObject];
gradient = [BackgroundLayer blueGradient];
[self.view.layer insertSublayer:gradient atIndex:0];
[self.view.layer setNeedsDisplay];
....

Not working.

Anyone can help me ? i need a solution for changing background color when i m on "near" and rechange it back when i m on "far".

Thanks in advance for your help.

Upvotes: 4

Views: 16544

Answers (1)

rdelmar
rdelmar

Reputation: 104092

I'm not using iBeacon, so in this test, I used a segmented control to switch between the layers. It should be easy enough to adapt this to your app.

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    CAGradientLayer *bg = [BackgroundLayer greyGradient];
    bg.frame = self.view.bounds;
    [self.view.layer insertSublayer:bg atIndex:0];
}

-(IBAction)changeGradient:(UISegmentedControl *)sender {

    CAGradientLayer *layerToRemove;
    for (CALayer *aLayer in self.view.layer.sublayers) {
        if ([aLayer isKindOfClass:[CAGradientLayer class]]) {
            layerToRemove = (CAGradientLayer *)aLayer;
        }
    }
    [layerToRemove removeFromSuperlayer];

    CAGradientLayer *bg;
    if (sender.selectedSegmentIndex == 0) {
        bg = [BackgroundLayer greyGradient];
    }else{
        bg = [BackgroundLayer blueGradient];
    }

    bg.frame = self.view.bounds;
    [self.view.layer insertSublayer:bg atIndex:0];
}

Upvotes: 4

Related Questions