我如何在iPhone开发中使这个animation效果? Quartz2d,核心animation?

我想做的是一个animation越来越大的酒吧,文字(数字)也随着酒吧长度的增长而增长。 看起来像:

在这里输入图像说明

酒吧应该是水平增长,与数字的文字也应该增长。 并且用户可能想要通过点击“重放”来再次播放animation。

阅读苹果编程指南和一些伟大的教程后,我有一个大致的想法。 使用Quartz2d,我可以绘制条和垂直线,也可以绘制文本。 但是Quartz2d没有animation效果。 使用Core Animation,我可以通过时间和指定的属性值来改变条的形状。 我对吗?

所以我想我想要的是结合Quartz2d和Core Animation,先用Quartz2d绘制条形图然后用CA来animation吧? 这是做到这一点的正确方法吗? 或者,如果有任何其他轻量级解决scheme,我将不胜感激。

PS:我对iPhone的绘图很新,据我所知,做animation的最轻的方法是通过UIViewanimation,更轻的是通过CALayeranimation。 而整个绘图工作是由Quartz2d,我正确吗? 即使阅读了这篇文章,这也是令人困惑的 。 但要实际操作,我不(或不能)对整个graphics系统有太多的概念。 但是在写下一些关于这个的实际代码后,我一定会继续挖掘。

所以,现在我只需要知道这是否是实现这个animation效果的最好方法。

多谢你们 :)

我会用我以下写的东西。 它会在1.5秒的时间内增加你的帧(当然,你可以改变持续时间)。 我不确定你的形状使用什么样的对象。 你可以使用像UIView东西,并设置backgroundColor到任何你想要的。

 // calculate your new frame - not sure what your parameters are as you've not explained it in your question. CGRect newFrame = CGRectMake(10, 10, 300, 40); // arbitrary values to demonstrate process... [UIView animateWithDuration:1.5 animations:^{ // grow your rect (assuming yourShape is a UIView) yourShape.frame = newFrame; yourLabel.center = CGPointMake(newFrame.size.width - yourLabel.frame.size.width, yourLabel.center.y) } completion:^(BOOL finished) { // do anything here that needs to occur after.. }]; 

不知道你是什么意思,“增长你的文字”(增加它或增加字体大小),所以我给了一个例子,将您的文字在你的水平栏的最后(右))。 这应该为UIViewanimation的实验提供一个很好的起点。


递增标签更新

您需要使用NSTimer来增加标签。 试试这个代码。

在你的.h

 int maxNum; NSTimer *numTimer; 

在你的.m

用这个来开始你的计数。 你可能会想把它放在你的UIViewanimation之前。

 maxNum = 100; // make this whatever number you need.. // you can change the TimeInterval to whatever you want.. numTimer = [NSTimer scheduledTimerWithTimeInterval:0.03 target:self selector:@selector(growNumbers:) userInfo:nil repeats:YES]; 

这是定时器在每次触发时调用的select器(每隔0.03秒):

 - (void)growNumbers:(id)sender { int i = self.numberLabel.text.intValue; if (i >= maxNum) { [numTimer invalidate]; maxNum = 0; } else { i++; self.numberLabel.text = [NSString stringWithFormat:@"%i",i]; } }