【问题标题】:Number increment animation while scrolling ios滚动ios时数字增量动画
【发布时间】:2013-08-21 19:47:29
【问题描述】:

我有一个UILabel,它将包含一个需要动画以从零增加到给定值的数字。标签放置在UITableView 内,是UITableViewCell 的子视图。由于某些奇怪的原因,我的数字在视图滚动时不会动画。如果我暂停滚动数字动画。为什么会这样以及如何解决?

方法 1 失败:

NSInteger fromValue = 0;
NSInteger toValue = 57; //In this example toValue has to be greater than fromValue
NSString *suffix = @"K";
NSTimeInterval interval = 0.016; //Adjust for different animation speed
NSTimeInterval delay = 0.0;
for (float i = fromValue; i <= toValue; i++)
{
    NSString *labelText = [NSString stringWithFormat:@"%0.3f%@", i, suffix];
    [numberLabel1 performSelector:@selector(setText:) withObject:labelText afterDelay:delay];
    delay += interval;
}

第二个是使用UICountingLabel from Giuthub,它提供了相同的功能但也不起作用。为什么?怎么修?我的使用代码如下:

UICountingLabel *numberLabel1 = [[UICountingLabel alloc] initWithFrame:CGRectMake(0, 105, winSize.width / 2, 40)]; //winSize is 
numberLabel1.textAlignment = NSTextAlignmentCenter;
numberLabel1.method = UILabelCountingMethodLinear;
numberLabel1.format = @"%.3f";
[numberLabel1 countFrom:0 to:num1.floatValue withDuration:countDuration];

关于如何使这个“滴答”的任何其他方式?我需要这个动画能够非常糟糕地滚动。

干杯,简。

【问题讨论】:

    标签: ios objective-c uitableview animation uiview


    【解决方案1】:

    您的UICountingLabel 方法可以工作。问题是计时器附加到的运行循环模式是默认的运行循环模式,并且在滚动正在进行时它不会运行。如果您修改计时器使其位于NSRunLoopCommonModes 中,那么它将触发并且标签将在滚动期间更新。


    UICountingLabel.m 中的第 137 行,将 NSDefaultRunLoopMode 替换为 NSRunLoopCommonModes

    【讨论】:

    • 你能解释一下怎么做吗?
    猜你喜欢
    • 2012-08-11
    • 2023-02-07
    • 2021-02-24
    • 1970-01-01
    • 2019-11-21
    • 2015-05-27
    • 1970-01-01
    • 1970-01-01
    • 2014-09-27
    相关资源
    最近更新 更多