【问题标题】:CAKeyframeAnimation for Sprite Animation精灵动画的 CAKeyframeAnimation
【发布时间】:2013-04-02 03:35:43
【问题描述】:

我正在尝试使用 Core Animation 为使用 Texture Packer 制作的精灵表制作动画。

我有一个包含所有精灵内容的 NSCollection。 (键和 CGI​​mageRef 的)

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"];

但它不起作用,屏幕上没有任何反应。有什么建议?谢谢!

【问题讨论】:

  • 为什么不使用带有动画图像的 UIImageView?
  • 哎哟!我不知道它存在!谢谢,我会调查的;)

标签: core-animation calayer sprite-sheet cakeyframeanimation texturepacker


【解决方案1】:

我的建议是“不要那样做”。您正在尝试实现一种技术,即 sprite sheet,用于解决与核心动画无关的问题。

精灵表的原因是为了处理 OpenGL(和其他图形 API)中的内存管理问题。 Core Animation 的工作方式不同,因此在 CA 中实现 sprite sheet 动画并不合适。

【讨论】:

    猜你喜欢
    • 2011-12-01
    • 2010-12-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多