0

Я пытаюсь анимировать спрайт-лист, сделанный с помощью Texture Packer, используя Core Animation.CAKeyframeAnimation для анимации Sprite

У меня есть NSCollection со всем содержанием спрайта. (Ключи и CGImageRef годы)

My Collection { 
"caja01.png" = "<UIImage: 0x1cda57c0>"; 
"caja02.png" = "<UIImage: 0x1cda61d0>"; 
"caja03.png" = "<UIImage: 0x1cda62e0>"; 
"caja04.png" = "<UIImage: 0x1cda63f0>"; 
"caja05.png" = "<UIImage: 0x1cda6540>"; 
"caja06.png" = "<UIImage: 0x1cda6650>"; 
"caja07.png" = "<UIImage: 0x1cda6760>"; 
"caja08.png" = "<UIImage: 0x1cda68f0>"; 
} 

И я пытаюсь анимировать с помощью CAKeyframeAnimation:

NSArray *values = [self.mySprites allValues]; // Get the CGImageRef's from NSDictionary 

// Creating a new layer 
CALayer *sublayer = [CALayer layer]; 
sublayer.backgroundColor = [UIColor blueColor].CGColor; 
sublayer.frame = imagen.frame; 
[self.view.layer addSublayer:sublayer]; 

// Create an first image from Sprite-sheet 
UIImageView *imagen = [[UIImageView alloc]initWithImage:[self.mySprites objectForKey:@"caja01.png"]]; 

// And add initial content to my layer 
sublayer.contents = (id) imagen.image.CGImage; 

// Try to make the animation 
CAKeyframeAnimation *animation = [CAKeyframeAnimation animationWithKeyPath: @"contents"]; 
animation.calculationMode = kCAAnimationDiscrete; 
animation.duration = 10.0; 
[animation setValues:values]; 
[animation setRepeatCount:2]; 
[animation setRemovedOnCompletion:NO]; 
[sublayer addAnimation: animation forKey: @"contents"]; 

Но это не работает, ничего не происходит на экране. Какие-либо предложения? Благодаря!

+1

Почему бы не использовать UIImageView с анимацией? –

+0

Ой! Я не знаю, что он существует! Спасибо, я буду исследовать;) –

ответ

1

Мое предложение - «не делай этого». Вы пытаетесь внедрить технику, спрайт-листы, которая предназначена для решения проблемы, не относящейся к Core Animation.

Причина для спрайтов заключается в решении проблем управления памятью в OpenGL (и других графических API). Core Animation работает по-разному, поэтому внедрение анимации спрайтов в CA не подходит.

+0

Спасибо. Некоторая альтернатива? –

Смежные вопросы