使用核心动画对图像进行排序,接收内存警告

时间:2013-09-23 08:42:41

标签: ios core-animation catransaction

我正在使用100个动画图像来接收内存警告,所以我尝试使用Core Animation,但这给了我同样的问题。这是因为我不知道如何在我当前的代码中使用replaceSublayer

UIView* upwardView=[[UIView alloc]init];
[upwardView setFrame:CGRectMake(0, 0, 1024, 768)];
[self.view addSubview:upwardView];

NSArray *animationImages=[NSArray arrayWithObjects:[UIImage imageNamed:@"001.png"],[UIImage imageNamed:@"001.png"],[UIImage imageNamed:@"002.png"],[UIImage imageNamed:@"003.png"],....,nil];

CAKeyframeAnimation *animationSequence = [CAKeyframeAnimation animationWithKeyPath: @"contents"];
animationSequence.calculationMode = kCAAnimationLinear;
animationSequence.autoreverses = YES;
animationSequence.duration = 5.00;
animationSequence.repeatCount = HUGE_VALF;

NSMutableArray *animationSequenceArray = [[NSMutableArray alloc] init];
for (UIImage *image in animationImages)
{
    [animationSequenceArray addObject:(id)image.CGImage];
}
CALayer *layer = [upwardView layer];
animationSequence.values = animationSequenceArray;
[layer addAnimation:animationSequence forKey:@"contents"];

1 个答案:

答案 0 :(得分:0)

我想您需要再添加几行。只需替换最后三行并添加以下行。

    //Prepare CALayer
    CALayer *layer = [CALayer layer];
    layer.frame = self.view.frame;
    layer.masksToBounds = YES;
    [layer addAnimation:animationSequence forKey:@"contents"];
    [upwardView.layer addSublayer:layer]; // Add CALayer to your desired view

有关详细实施,请查看此 reference