iOS - 多层使动画变慢

时间:2012-01-16 15:49:46

标签: ios performance animation calayer

我有一个带有许多CAShapeLayers的子视图我动画,这使得动画变慢。除此之外,动画应该按顺序执行,但我不知道为什么,它们几乎同时被执行。有什么我可以改变来解决这些问题吗? (我正在使用animateWithDuration)。

段:

for (id layer in self.layer.sublayers) {
...
   [UIView animateWithDuration:1.5f
                         delay:0.0f 
                       options: UIViewAnimationOptionAllowUserInteraction | UIViewAnimationOptionTransitionCrossDissolve
                    animations:^{
                                subLayer.affineTransform = CGAffineTransformScale(subLayer.affineTransform, 1.5, 1.5);
                                 [subLayer setZPosition:1000];    
                                 }
                    completion:^(BOOL finished) {
                        [UIView animateWithDuration:1.0f
                                              delay:1.5f 
                                            options: UIViewAnimationOptionAllowUserInteraction
                                         animations:^{ subLayer.affineTransform = CGAffineTransformIdentity;
                                         }
                                         completion:nil
                                      ];

                                 }];
...
}

我改变了我的动画方式,但仍然不顺利:

NSOperationQueue *queue = [NSOperationQueue new];

NSInvocationOperation *operation = [[NSInvocationOperation alloc] initWithTarget:self 
                                                                        selector:@selector(newCicle) 
                                                                          object:nil];
[queue addOperation:operation];

[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:0.3f];
[UIView setAnimationCurve: UIViewAnimationCurveLinear];

int layerNumber = 1;
for (id layer in self.layer.sublayers) {
    if ([layer isKindOfClass:[CAShapeLayer class]]) {
        int k = layerNumber % SQHOR;
        int i = ceil((float)layerNumber / (float)SQHOR);
        CGColorRef color = nil;

        if (changed[i][k]) {
            if (matrix[i][k]) {
                color = [[UIColor redColor] CGColor];
            }
            else {
                color = [[UIColor whiteColor] CGColor];
            }

            CAShapeLayer *subLayer = (CAShapeLayer*)layer;                                             

            subLayer.fillColor = color;

        }
        layerNumber++;
    }

}

[UIView commitAnimations];    

2 个答案:

答案 0 :(得分:1)

如果动画应该按顺序执行,计算你的每个动画延迟时间,它应该是这样的:

CGFloat everySubLayerAnimationDuration = 1.5f; // or other 
for (int i = 0; i < sublayers.count; i++)
{
    [UIView animateWithDuration: everySubLayerAnimationDuration * i
                     delay:0.0f  
    ......
}

答案 1 :(得分:0)

尝试将setShouldRasterize:YES设置为您的图层或您的超级图层。