2013-06-24 58 views
0

我使用的CALayer背景:CALayer的背景顏色

CAGradientLayer *bgLayer = [BackgroundLayer blueGradient]; 
bgLayer.frame = self.view.bounds; 
[self.view.layer insertSublayer:bgLayer atIndex:0]; 

- (void)prepareToRotate:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration 

我用這條線旋轉CALayer的背景。

[[[self.view.layer sublayers] objectAtIndex:0] setFrame:self.view.bounds]; 

我得到了一些撕裂的效果是不漂亮的層看似不旋轉速度不夠快,我怎麼能解決這個問題,並獲得旋轉無縫效果,有沒有更好的方式來調整的CALayer ?

感謝,

編輯:我的所有代碼:

的.H:

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


@interface BackgroundLayer : NSObject 

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

@end 

的.M

#import "BackgroundLayer.h" 

@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 

準備旋轉僅僅通過

稱爲
- (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation 
{ 
    [self prepareToRotate:[[UIApplication sharedApplication] statusBarOrientation] duration:0]; 

} 

和只包含

[[[self.view.layer子層] objectAtIndex:0] SETFRAME:self.view.bounds];

回答

2

我建議使用CoreGraphics的CAGradientLayer。渲染速度要快得多。對於您的問題,請嘗試在旋轉之前柵格化您的視圖。

- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration 
{ 
    self.bgLayer.shouldRasterize = YES; 
} 

實際上,您可以添加子視圖而不是新圖層。

@interface MyView : UIView 

@end 

@implementation MyView 

+ (Class)layerClass 
{ 
    return [CAGradientLayer class]; 
} 

@end 

在您的舊插入圖層部分中,請改爲執行下列操作。

MyView *bgView = [[MyView alloc] initWithFrame:self.view.bounds]; 
[(CAGradientLayer *)bgView.layer setColors:colors]; 
bgView.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth; 

[self.view insertSubview:bgView atIndex:0]; 

(你可以使用自動佈局,如果你想)

+0

遺憾的是沒有工作,什麼shouldRasterize辦?我有一個 - (void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation持續時間:(NSTimeInterval)持續時間 我認爲超過WillRotateToInterface的方法... – Woodstock

+0

注意:frame屬性不是直接可以動畫的。相反,您應該爲邊界,anchorPoint和位置屬性的適當組合設置動畫效果,以獲得所需的結果。 –

1

Coregraphics漸變層比CALayer好得多。 CALayer速度較慢,將顯示梯度波段,而核心圖將具有平滑的梯度。這可能是爲什麼它發生在你身上。

創建一個 - (void)drawRect:(CGRect)rect {函數並在那裏繪製您的漸變。它應該照顧你的問題。

這裏有很多示例代碼。這裏有一些

CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB(); 
CGFloat locations[] = { 0.0, 1.0 }; 

NSArray *colors = @[(__bridge id) startColor, (__bridge id) endColor]; 

CGGradientRef gradient = CGGradientCreateWithColors(colorSpace, (__bridge CFArrayRef) colors, locations);