Animating a UITextView increasing or decreasing the value of a number

ぃ、小莉子 提交于 2019-12-11 07:41:48

问题


I have a UITextView that displays a value (ex. "32039"). Frequently in my app I change the value of that number, but want to animate the increase or decrease in value. The animation I have in mind is something like this. I've seen this question but the only animations possible using the method described in the answers are those like fade in/out, move in from left/right/down/up, and reveal/push. Is there a UIKit or Core Animation implementation of this animation, or even an implementation in a 3rd party library, or should I roll my own?


回答1:


// Something like this would work

@property (nonatomic, strong) UILabel *testLabel;

@property (nonatomic, strong) NSTimer *timer;

@property (nonatomic, assign) NSUInteger counter;

- (void)viewDidLoad
{
    [super viewDidLoad];

    self.testLabel = [[UILabel alloc] initWithFrame:CGRectMake(50.0f, 100.0f, 80.0f, 25.0f)];
    self.testLabel.text = @"44";
    [self.view addSubview:self.testLabel];

    self.timer = [NSTimer timerWithTimeInterval:.5 target:self selector:@selector(changeLabelText) userInfo:nil repeats:YES];
    [[NSRunLoop currentRunLoop] addTimer:self.timer forMode:NSDefaultRunLoopMode];
    [self.timer fire];
}

- (void)changeLabelText {
    self.counter++;
    if (self.counter == 100000) {
         [self.timer invalidate];
    }

    self.testLabel.text = [NSString stringWithFormat:@"%d", (44 + self.counter)];
}


来源:https://stackoverflow.com/questions/18807849/animating-a-uitextview-increasing-or-decreasing-the-value-of-a-number

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!