使用CAGradientLayer classe更改UiView IOS的​​渐变背景图层颜色

时间:2015-02-04 17:39:15

标签: ios objective-c cocoa-touch uiview ibeacon

我想在触发动作时更改渐变背景的颜色。 我尝试过很多方法而没有成功。

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

在我的视图控制器中,我有一个接近开关(我使用ibeacon)。 开关有4个案例。我希望Case 1(Far)有蓝色渐变色。情况2(近)灰色渐变。我初始化&#34;(void)viewWillAppear:(BOOL)动画&#34;然后我在do范围代表中尝试了这个:

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];
....

不工作。 我也尝试过这样的

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];
....

不工作。

任何人都可以帮助我吗?当我在&#34;附近时,我需要一个改变背景颜色的解决方案。当我在&#34;远&#34;。

时重新改变它

提前感谢您的帮助。

1 个答案:

答案 0 :(得分:4)

我没有使用iBeacon,所以在这个测试中,我使用分段控件在各层之间切换。它应该很容易适应你的应用程序。

@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];
}