加载文本中的动画点

时间:2014-05-23 13:01:45

标签: ios7

我想加载动画。当我点击按钮时,文字变为" loading"然后"加载。"那么" loading .."从一开始。这该怎么做? 我试过了[UIView animateWithDuration],但它没有帮助。

1 个答案:

答案 0 :(得分:3)

实现起来并不困难。创建一个NSTimer属性,然后创建启动/停止/更新方法,如下所示:

- (void)startLoadingAnimation {
    [self.myButton setTitle:@"Loading" forState:UIControlStateNormal];
    self.myTimer = [NSTimer scheduledTimerWithTimeInterval:0.5f target:self selector:@selector(updateLoadingLabel) userInfo:nil repeats:YES];
}

- (void)updateLoadingLabel {
    NSString *ellipses = @"...";

    if ([self.myButton.titleLabel.text rangeOfString:ellipses].location == NSNotFound) {
        [self.myButton setTitle:[NSString stringWithFormat:@"%@.",self.myButton.titleLabel.text] forState:UIControlStateNormal];
    } else {
        [self.myButton setTitle:@"Loading" forState:UIControlStateNormal];
    }
}

- (void)endLoadingAnimation {
    [self.myTimer invalidate];
    self.myTimer = nil;
    [self.myButton setTitle:@"Loading" forState:UIControlStateNormal];
}

在此示例中,标签每0.5秒更新一次。