iPhone使用进度条达到计时器流畅的动画效果 [英] iPhone achieve smooth animation using progress bar timer

查看:623
本文介绍了iPhone使用进度条达到计时器流畅的动画效果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想实现在学习的精神iphone的一个简单的测验应用程序,这个应用程序的一部分,是一个计时器。

I am trying to implement a simple quiz app on the iphone in the spirit of learning, part of this app is a timer.

我想我的定时器倒计时从10到0。我有一个简单的NSTimer的重复和调用方法每一秒,在这种方法我更新它显示剩余时间的标签,这很好地工作。

I want my timer to count down from 10 to 0. I had a simple NSTimer that repeats and calls a method every second and in this method I update a label which displays the remaining time, which works nicely.

现在,而不是使用一个标签,我想用一个图形化的进度条显示计时器,所以我创建了十张图片,一个完整长度(重presenting 10),尺寸的下一个9/10等对了,在我重复定时器的方法,而不是更新的标签我更新相应的图像一个UIImage等过段时间进度条变得越来越小。

Now instead of displaying the timer using a label I wanted to use a graphical progress bar, so I created ten images, one full in length (representing 10), the next 9/10 of the size and so on, and in my repeating timer method instead of updating a label I update a UIImage with the appropriate image so over time the progress bar gets smaller and smaller.

我的问题是,由于我的方式已经实现了进度条,它看起来并不很流畅时,它更新每一秒。有另一种方法我应该接近开发这种功能?我听说,你可以使用可伸缩的图像获得平滑的效果,但我不能看到任何很好的例子。

My problem is that due to the way I have implemented the progress bar, it doesn't look very smooth when it updates every second. Is there another way I should approach developing this kind of functionality? I have heard that you could use a stretchable image to get a smoother effect but I couldn't see any good examples of that.

任何意见和code样品的欢迎,只是想在这里学习。

Any advice and code samples welcome, just trying to learn here.

推荐答案

调查我决定用一个UIProgressView几个选项后(在评论须藤建议),再加上的NSTimer其更新进度十次十第二秒,我用这个解决方案,因为:

After investigating a few options I have decided to use a UIProgressView (suggested by sudo in comments) coupled with a NSTimer which updates the progress ten times a second for ten seconds, I used this solution because:


  1. 它是由苹果公司提供的标准元素。

  2. 此选项没有创建和加载100张图像(如氪在评论建议)的开销。

  3. 将在我的NSTimer可以添加一个检查功能updateProgressBar一些反馈添加到用户当定时器达到低,E.E.振动的最后3秒等(使用动画,我不认为我会这样的选择)。

使用code以下假设我有一个名为progressView,一个名为的NSTimer定时器一个UIProgressView变量并命名为时间一个浮点变量,那么:

Using the code below assume I have a UIProgressView variable named 'progressView', an NSTimer named 'timer' and a floating point variable named 'time', then:

-(void)updateProgressBar
{
    if(time <= 0.0f)
    {
        //Invalidate timer when time reaches 0
        [timer invalidate];
    }
    else
    {
        time -= 0.01;
        progressView.progress = time;
    }
}

-(void)animateProgressView2
{
    progressView.progress = 1;
    time = 1;
    timer = [NSTimer scheduledTimerWithTimeInterval: 0.1f
                                             target: self
                                           selector: @selector(updateProgressBar)
                                           userInfo: nil
                                            repeats: YES];
}

我调查(看到迪斯科的评论之后),但决定对另一种方法是使用动画。在这种情况下我创建了两个png图片,名为redbar.png'和'greenbar.png',这个想法让他们将被放置在彼此(绿色条是在顶部/在前台),超过期限动画绿坝缩小,揭示在背景中的红色吧。

Another way I investigated (after seeing Disco's comments) but decided against was to use animation. In this instance I created two png images, named 'redbar.png' and 'greenbar.png', the idea bring they would be placed over each other (the green bar being on top/in the foreground), and over the duration of the animation the green bar shrinks, revealing the red bar in the background.

UIImage *green= [UIImage imageNamed:@"greenbar.png"];
UIImage *red= [UIImage imageNamed:@"redbar.png"];

redBar.image = red; // allocate the red image to the UIImageView
redBar.frame = CGRectMake(20,250,220,30);// set the size of the red image

greenBar.image = green; // allocate the green image to the UIImageView
greenBar.frame = CGRectMake(20,250,220,30);//set the size of the green image

[UIView beginAnimations:nil context:nil];
[UIView setAnimationDelegate:self];
[UIView setAnimationDuration:10]; //the time you want the animation to last for
[UIView setAnimationDidStopSelector:@selector(animationFinished)];//the method to be called once the animation is finished
    //at the end of the animation we want the greenBar frame to disappear
    greenBar.frame = CGRectMake(20,250,0,30);
[UIView commitAnimations];

这是理想的解决方案是将UIProgressView和UIAnimation块结合起来,但是这目前还不是一个选项(参见这个问题的动画一个UIProgressView的变化)。

An ideal solution would be to combine the UIProgressView and the UIAnimation block, but this is not currently an option (see this question animate a UIProgressView's change).

希望这可以帮助别人的未来。

Hope this helps someone in the future.

编辑:

它出现的理想的解决方案,我上面提到的是现已在iOS5中,从苹果网站引用次数:

It appears the ideal solution I mention above is now available in iOS5, taken from the apple reference site:

setProgress:动画:
调节接收机显示,可选动画改变目前的进展。

setProgress:animated: Adjusts the current progress shown by the receiver, optionally animating the change.


  • (无效)setProgress:(浮点)进度动画:(BOOL)动画

这篇关于iPhone使用进度条达到计时器流畅的动画效果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆