gpt4 book ai didi

ios - CALayer 背景色

转载 作者:行者123 更新时间:2023-11-28 20:17:40 27 4
gpt4 key购买 nike

我有一个 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];

最佳答案

我建议使用 CAGradientLayer 而不是 CoreGraphics。渲染速度要快得多。对于您的问题,请尝试在旋转之前栅格化您的 View 。

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

实际上,您可以添加 subview 而不是新层。

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

(如果你愿意,你可以使用自动布局)

关于ios - CALayer 背景色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17280522/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com