使用 NSTimer 显示像汽油泵仪表一样动画的 StopWatch Timer [英] Display StopWatch Timer animated like the petrol pump meter using NSTimer

查看:44
本文介绍了使用 NSTimer 显示像汽油泵仪表一样动画的 StopWatch Timer的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是 iOS 开发新手.当我按下秒表开始按钮时,我想像计数器令牌效果一样显示计时器.我附上了图片供您参考.我已经完成了显示秒和分钟的工作,但我不知道,如何动画自动滚动效果?我该怎么做?

I am new in iOS developement.When I press the stopwatch start button I want to display the timer like counter token effect.I have attached image for your reference.I have done to display secs and minutes but I dont know, How animate autoscroll effect? How can I do this?

当计数器移动时,它不应该从一个数字跳到另一个数字,而是应该从一个数字平滑地移动到下一个数字,就像汽油泵仪表一样.谢谢

When the counter is moving it shouldn't jump from one number to another number, instead it should move from one number to next number smoothly, just like the petrol pump meter. Thanks

推荐答案

你需要手动滚动 tableView 而不是 scrollToRowAtIndexPath 因为这个动画使用它自己的计时器间隔并且它非常困难或者我们可以说不可能改变它的时间间隔.

You need to manually scroll tableView instead of scrollToRowAtIndexPath because this animation uses its own timer interval and its very difficult or we can say impossible to change its time interval.

因此,我正在为此类问题实现 API,并为您制作了一个演示应用程序,可以根据需要平滑滚动.

So, I am Implementing an API for such kind of problems and made a demo app for you with smooth scrolling as you want.

您需要使用每 1 秒触发一次的外部计时器和每 0.03 秒触发一次的内部计时器,因为我的 tableRow 高度为 30 我计算的内部计时器间隔为:---

You need to use an outer timer that fires every 1 second and an internal timer that will fire every 0.03 sec as my tableRow Height is 30 I calculated internal timer interval as :---

移动 30 个像素 1 秒,然后在内部计时器内移动 1 个像素 0.33 秒.

Move 30 pixels for 1 sec , then Move 1 pixel for 0.33 sec inside internal timer.

内部计时器将在外部计时器内初始化时每 1 秒失效并触发一次.

The Internal timer will invalidate and fire every 1 second as initialized within outer timer.

内部定时器将执行单元格的移动.

And internal timer will perform the movement of cell.

dial4 是一个 tableView

看看我的代码.

#define rowHeight 30

-(void)startCounter
{
    outertimer=[NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(snapCell) userInfo:nil repeats:YES];
}

-(void)stopCounter
{
    [outertimer invalidate];
}

-(void)snapCell
{
    NSLog(@"Initialize Internal timer %i",secLsb);
    secLsb++;

    if (secLsb==10) {
        [dial4 setContentOffset:CGPointMake(0, 0) animated:NO];
        secLsb=0;
         NSLog(@"dial content offset y is %f",dial4.contentOffset.y);
    }

    [internaltimer invalidate];
    internaltimer=[NSTimer scheduledTimerWithTimeInterval:0.03
                                     target:self
                                   selector:@selector(automaticScroll)
                                   userInfo:nil
                                    repeats:YES];
}

-(void)automaticScroll
{
    NSLog(@"val is & y ======== %f",dial4.contentOffset.y);

    [dial4 setContentOffset:CGPointMake(dial4.contentOffset.x,dial4.contentOffset.y+1) animated:NO];
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return rowHeight;
}

看看时间计数器

这篇关于使用 NSTimer 显示像汽油泵仪表一样动画的 StopWatch Timer的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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