我witing IOS应用程序时,接口由许多CALayers呈现。有一次,我注意到,CPU被其他进程(实际上不是APP)在图形更新加载。我开始的接口部分禁用更新和去的时刻只有一个CALayer的(在50-60赫兹)进行了更新,但所有其他层(数百个)进行静态展示过。所以仅更新这个一层的由其他进程收费CPU负荷的60-70%。当这个唯一的层的禁用更新,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的内容,其设置为一些图片
NOTE_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
正如评论说以上,创下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], };