为什么更新CALayer会导致CPU使用率(通过其他进程)增加60-70%?

时间:2019-02-08 10:34:22

标签: ios performance core-animation calayer cpu-usage

当许多 CALayers 呈现界面时,我正在使用 IOS 应用程序。我注意到图形更新期间,其他进程(实际上不是应用程序)加载了 CPU 。我开始禁用界面部件的更新,直到只有一个 CALayer 被更新( 50-60 Hz ),但是所有其他层(数百个)都静态地更新了也显示。因此,仅此ONE层的更新将占用其他进程 60-70%的CPU 负载。禁用仅此层的更新时,不会加载 CPU

任何人都可以说这是怎么回事吗?

[CATransaction begin];
[CATransaction setValue:(id)kCFBooleanTrue forKey:kCATransactionDisableActions];

// update layer which is sublayer of self.view.layer
self.headingCircularScaleLayer.transform = CATransform3DMakeRotation(DegToRad(-newHeadingAngle_deg), 0, 0, 1);

[self.view.layer setNeedsLayout];

[CATransaction commit];

此处 headingCircularScaleLayer 是CALayer,其内容设置为某些图像

注意_1:(通过添加多层并更新它们来测试CPU负载)

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
}

-(void) viewDidAppear:(BOOL)animated {

    [self createAndSetupLayers];

    CADisplayLink *displayLink = [CADisplayLink displayLinkWithTarget:self selector:@selector(updateLayers)];
    [displayLink addToRunLoop:[NSRunLoop mainRunLoop] forMode:NSDefaultRunLoopMode];

    self.startTime = [[NSDate date] timeIntervalSince1970];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


-(void) createAndSetupLayers {

    NSMutableArray<CALayer*>* newLayersArray = [[NSMutableArray alloc] init];

    long numOfRows = 28;
    long numOfCols = 21;
    long numOfLayers = numOfRows * numOfCols;

    CGFloat cellWidth = self.view.bounds.size.width / numOfCols;
    CGFloat cellHeight = self.view.bounds.size.height / numOfRows;

    CGFloat layerWidth = cellWidth * 0.9;
    CGFloat layerHeight = cellHeight * 0.9;

    long currRow = 0;
    long currCol = 0;

    for (long i = 0; i < numOfLayers; i++)
    {
        currRow = i / numOfCols;
        currCol = i % numOfCols;

        CALayer* newLayer = [[CALayer alloc] init];

        newLayer.bounds = CGRectMake(0.0, 0.0, layerWidth, layerHeight);
        newLayer.anchorPoint = CGPointMake(0.5, 0.5);
        newLayer.position = CGPointMake((currCol + 0.5) * cellWidth, (currRow + 0.5) * cellHeight);
        newLayer.backgroundColor = [UIColor greenColor].CGColor;
        //newLayer.opacity = 0.5;

        //NSDictionary *newActions = @{ @"transform": [NSNull null] };
        //newLayer.actions = newActions;
        newLayer.actions = @{ @"contents": [NSNull null], @"position": [NSNull null],
                           @"frame": [NSNull null], @"opacity": [NSNull null],
                           @"bounds": [NSNull null], @"affineTransform": [NSNull null],
                           @"sublayerTransform": [NSNull null], @"transform": [NSNull null],
                           @"zPosition": [NSNull null], @"anchorPoint": [NSNull null],
                           @"cornerRadius": [NSNull null], @"sublayers": [NSNull null],
                           @"onLayout": [NSNull null], };


        CALayer* newColorLayer = [[CALayer alloc] init];
        newColorLayer.bounds = CGRectMake(0.0, 0.0, layerWidth, layerHeight);
        newColorLayer.anchorPoint = CGPointMake(0.5, 0.5);
        newColorLayer.position = CGPointMake(0.5 * layerWidth, 0.5 * layerHeight);
        newColorLayer.opacity = 0.5;
        newColorLayer.opaque = YES;
        newColorLayer.backgroundColor = [UIColor redColor].CGColor;

        //[newLayer addSublayer:newColorLayer];
        [self.view.layer addSublayer:newLayer];
        [newLayersArray addObject:newLayer];
    }

    self.layersArray = newLayersArray;
}

-(void) updateLayers {
    double currTime = [[NSDate date] timeIntervalSince1970] - self.startTime;

    double currLayerAngle = 0.5 * currTime;

    NSLog(@"%.2f  %.2f", currTime, currLayerAngle);

    //[CATransaction begin];
    //[CATransaction setValue:(id)kCFBooleanTrue forKey:kCATransactionDisableActions];
    //[CATransaction setAnimationDuration:0.0];

    for (CALayer *currLayer in self.layersArray)
    {
        currLayer.transform = CATransform3DMakeRotation(currLayerAngle, 0, 0, 1);
    }

    //[CATransaction commit];
}

@end

1 个答案:

答案 0 :(得分:0)

如以上评论所述,设置kCATransactionDisableActions仅会影响当前交易。您可能在其他地方有启动隐式动画的代码。尝试将图层操作明确设置为null。创建图层后,您只需一次。在这种情况下,您不需要进行显式事务。

layer.actions = @{ @"contents": [NSNull null], @"position": [NSNull null],
                   @"frame": [NSNull null], @"opacity": [NSNull null],
                   @"bounds": [NSNull null], @"affineTransform": [NSNull null],
                   @"sublayerTransform": [NSNull null], @"transform": [NSNull null],
                   @"zPosition": [NSNull null], @"anchorPoint": [NSNull null],
                   @"cornerRadius": [NSNull null], @"sublayers": [NSNull null],
                   @"onLayout": [NSNull null], };