2013-10-15 31 views
2

我已经看过动画了,我无法弄清楚如何在这个video(youtube)中做类似的事情。我想讨论它是如何制造的。我不认为他们在使用精灵。我有一个想法如何做到这一点:例如,我想创建“走路”动物动画(当动物移动时,他的腿“跑步”移动动画),我应该创建自定义动物身体视图3和两个imageViews动物腿2。然后,我制作了移动腿的硬编码动画,瞧,我有自定义动画。当我移动customView时,我应该开始动画腿。但有没有更好的方法来做到这一点?谢谢!定制动画的对象(动物)

enter image description here enter image description here

+0

你或许应该考虑雪碧处理现有的库。我使用cocos2d。然而,你的问题是基于意见的,而我即将关闭它。[programmer.se] –

+0

可能更好,这只是我想知道如何使用UIKit – ignotusverum

+0

SpriteKit可能更多使用 –

回答

2

对于一个简单的效果,你可以保存左脚和右脚作为单独的图像,然后你可以调用重复,autoreversing动画独立地,每个旋转腿部图像的属性,例如,是这样的:

// start one foot animation immediately 

[UIView animateWithDuration:0.25 animations:^{ 
    self.leftFootImageView.transform = [self calculateTransformForFootToAngle:M_PI/8.0]; 
} completion:^(BOOL finished) { 
    [UIView animateWithDuration:0.5 delay:0.0 options:UIViewAnimationOptionRepeat | UIViewAnimationOptionAutoreverse animations:^{ 
     self.leftFootImageView.transform = [self calculateTransformForFootToAngle:-M_PI/8.0]; 
    } completion:NULL]; 
}]; 

// delay the second foot's animation a bit, if you want 
// (if you don't want to change the timing of the right foot, you 
// could just combine the setting of the right foot's `transform` 
// in the above block) 

double delayInSeconds = 0.1; 
dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, (int64_t)(delayInSeconds * NSEC_PER_SEC)); 
dispatch_after(popTime, dispatch_get_main_queue(), ^(void){ 
    [UIView animateWithDuration:0.25 animations:^{ 
     self.rightFootImageView.transform = [self calculateTransformForFootToAngle:M_PI/8.0]; 
    } completion:^(BOOL finished) { 
     [UIView animateWithDuration:0.5 delay:0.0 options:UIViewAnimationOptionRepeat | UIViewAnimationOptionAutoreverse animations:^{ 
      self.rightFootImageView.transform = [self calculateTransformForFootToAngle:-M_PI/8.0]; 
     } completion:NULL]; 
    }]; 
}); 

显然,你需要计算CGAffineTransform值的方法:

- (CGAffineTransform)calculateTransformForFootToAngle:(CGFloat)angle 
{ 
    // translate up, rotate, and then translate back, so that the rotation takes place at the top of the leg 

    CGAffineTransform transform = CGAffineTransformMakeTranslation(0, -15); 
    transform = CGAffineTransformRotate(transform, angle); 
    return CGAffineTransformTranslate(transform, 0, 15); 
} 

上述假定您已经定义了个人的脚的图像是非常小的,只是边界脚,而不是所有的空白。您必须充分利用适合您图片的翻译值。

最后,当你想停止动画,然后你可以只是动画双腿回到恒等变换:

[UIView animateWithDuration:0.25 delay:0.0 options:UIViewAnimationOptionBeginFromCurrentState animations:^{ 
    self.leftFootImageView.transform = CGAffineTransformIdentity; 
    self.rightFootImageView.transform = CGAffineTransformIdentity; 
} completion:NULL]; 
+0

哇,谢谢。但是我想知道,例如,我会有10-15个这样的对象,它会变得迟钝吗? – ignotusverum

+0

@anonymous只要你正在执行这个'transform'就是小图像视图,它应该非常高效。我有64条腿在iPhone 3GS上以不同的速度摆动,并且在60fps时非常平滑。当然,在某些情况下,你最终会遇到退化,但它效率惊人(如果你小心)。 – Rob

+1

@anonymous顺便说一句,看这段视频,他们可能也会使用'CAKeyFrame'动画(例如,向一个方向摆动一点,然后向另一个方向摆动一下,然后重复)。 – Rob

2

你必须要使用的每一个照片。因此,在Photoshop(或您的软件)中,您可以让动画“跑步”的动画显示30张图片,所有分辨率和尺寸都相同(在每张照片中移动腿部)您只需要一个图像视图,而不是3个。请记住,您的常规视频是每秒30帧。所以如果你想让这个动画播放一秒钟,你会想要至少有20张图片。继承人,你要去的代码想在.m文件使用方法:

- (IBAction)animation{ 
animationView.animationImages = [NSArray arrayWithObjects: 
            [UIImage imageNamed:@"PR-P-1"], //use your image names here, put the names in in order, I recommend making the images initially with a number to tell you which one in the sequence it is 
            [UIImage imageNamed:@"PR-P-2"], 
            [UIImage imageNamed:@"PR-P-3"], 
            [UIImage imageNamed:@"PR-P-4"], 
            [UIImage imageNamed:@"PR-P-5"], 
            [UIImage imageNamed:@"PR-P-6"], 
            [UIImage imageNamed:@"PR-P-7"], 
            [UIImage imageNamed:@"PR-P-8"], 
            [UIImage imageNamed:@"PR-P-9"], 
            [UIImage imageNamed:@"PR-P-10"], 
            [UIImage imageNamed:@"PR-P-11"], 
            [UIImage imageNamed:@"PR-P-12"], 
            [UIImage imageNamed:@"PR-P-13"], 
            [UIImage imageNamed:@"PR-P-14"], 
            [UIImage imageNamed:@"PR-P-15"], 
            [UIImage imageNamed:@"PR-P-16"], 
            [UIImage imageNamed:@"PR-P-17"], 
            [UIImage imageNamed:@"PR-P-18"], 
            [UIImage imageNamed:@"PR-P-19"], 
            [UIImage imageNamed:@"PR-P-20"], 
            [UIImage imageNamed:@"PR-P-21"], 
            [UIImage imageNamed:@"PR-P-22"], 
            [UIImage imageNamed:@"PR-P-23"], 
            [UIImage imageNamed:@"PR-P-24"], 
            [UIImage imageNamed:@"PR-P-25"], 
            [UIImage imageNamed:@"PR-P-26"], 
            [UIImage imageNamed:@"PR-P-27"], 
            [UIImage imageNamed:@"PR-P-28"],nil]; //make sure to have nil! 


animationView.animationRepeatCount = 1; //sets how many times it repeats, do a huge number for infinate 
animationView.animationDuration = 2.5; //sets how long one repeat takes 
[animationView startAnimating]; //starts the animation 

} 

然后在您的.h文件中声明IBOutlet这应该是一个UIImageView名为animationView

如果想要另一个动画,只是使用与上面相同的代码,更改IBAction的名称并在您的.h文件中创建另一个IBOutlet,该文件将再次为UIImageView,并将其命名为IBAction,例如,如果您制作了另一个IBAction称为playViolin,你将在你的.h文件中使用IBOutlet playViolin

然后在您的.xib文件中,创建一个新的图像视图,并从文件的所有者挂接animationView并将其设置。当你想让它开始时,请致电[self animation]

1

这是一个很好的教程它在哪里好explaine:

http://www.appcoda.com/ios-programming-animation-uiimageview/

+1

虽然此链接可能回答问题,但最好在此处包含答案的基本部分并提供供参考的链接。如果链接页面更改,则仅链接答案可能会失效。 - [来自评论](/ review/low-quality-posts/18997573) – dpassage