【问题标题】:CALayer background colorCALayer 背景颜色
【发布时间】:2013-06-24 16:36:01
【问题描述】:

我有一个 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 sublayers] objectAtIndex:0] setFrame:self.view.bounds];

【问题讨论】:

    标签: ios objective-c background calayer


    【解决方案1】:

    我建议在 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];
    

    (如果需要,您可以使用自动布局)

    【讨论】:

    • 很遗憾没用,shouldRasterize 怎么办?我已经有一个 - (void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration 方法,我认为它取代了 WillRotateToInterface...
    • 注意:frame 属性不能直接设置动画。相反,您应该对 bounds、anchorPoint 和 position 属性的适当组合进行动画处理,以实现所需的结果。
    【解决方案2】:

    Coregraphics 渐变层比 CALayer 好得多。 CALayer 很慢并且会显示渐变带,而 coregraphics 将具有平滑的渐变。这可能就是它发生在你身上的原因。

    创建一个 -(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);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-10-04
      • 1970-01-01
      • 1970-01-01
      • 2015-05-17
      • 1970-01-01
      • 1970-01-01
      • 2016-04-07
      • 1970-01-01
      相关资源
      最近更新 更多