我希望当我触发一个操作时,可以改变我的渐变背景的颜色。我已经尝试了很多方法,但都没有成功。
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种情况。我希望第一种情况(远离)是蓝色渐变的,第二种情况(接近)是灰色渐变的。我在“(void)viewWillAppear:(BOOL)animated”方法中初始化了这个层,然后我尝试在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];
....
无法工作。 我也尝试了这样做
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];
....
无法工作。
有人能够帮忙吗?我需要一种解决方案,可以在我处于“附近”时更改背景颜色,并在我处于“远离”时重新更改回来。
非常感谢您的帮助。
self.view
中,从而容纳所有内容,然后只需将self.view
的图层更改为渐变即可。 - Ian MacDonald