如何创建iOS简单文本动画?

时间:2013-01-08 15:31:29

标签: ios animation xcode4.5

简单的iOS文本/标签动画的最佳解决方案是什么?细节:用户需要在iOS屏幕上看到一个标签,其中包含来自阵列10-15个单词的更改文本,一个单词由白色屏幕分隔。显示时间一个单词为800-900毫秒,显示时间为白色屏幕是500毫秒。有一种注意力测试。

2 个答案:

答案 0 :(得分:3)

如果您只想点击字词,可以使用NSTimerperformSelector

@interface ViewController ()

@property (nonatomic, strong) NSArray *words;
@property (nonatomic) NSUInteger wordIndex;

@end

@implementation ViewController

static CGFloat const kWordShowInterval = 0.8;
static CGFloat const kWordHideInterval = 0.4;

- (void)viewDidLoad
{
    [super viewDidLoad];

    self.words = @[@"one", @"two", @"three", @"four"];
    self.wordIndex = 0;

    [self showWord];
}

- (void)showWord
{
    if (self.wordIndex >= [self.words count])
        return;

    self.wordLabel.text = self.words[self.wordIndex];

    [self performSelector:@selector(hideWord)
               withObject:nil
               afterDelay:kWordShowInterval];
}

- (void)hideWord
{
    self.wordLabel.text = nil;

    self.wordIndex++;
    if (self.wordIndex < [self.words count])
    {
        [self performSelector:@selector(showWord)
                   withObject:nil
                   afterDelay:kWordHideInterval];
    }
    else
    {
        // all done, go ahead and invoke whatever you want to do when done presenting the words
    }
}

@end

如果你想对文本的外观或消失做一些动画(例如淡入或淡出),你可以将它与animateWithDuration或其他动画结构结合起来。

答案 1 :(得分:0)

通常核心动画是最好的方式。查看the documentation