2013-01-08 31 views
1

什麼是簡單的iOS文本/標籤動畫的最佳解決方案?詳細信息:用戶需要在iOS屏幕上看到一個標籤,其數組中的文字更改爲10-15個字,一個字由一個白色的屏幕分隔開。顯示時間一個字爲800-900毫秒,顯示時間爲白色屏幕是500毫秒。有一種注意力測試。如何創建iOS簡單的文本動畫?

+0

我以前只用NSTimer做過。它工作得很好。 –

回答

3

如果你只是想閃光的話,你可以使用NSTimer或只是performSelector

@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或其他動畫構造結合使用。

+0

羅布,謝謝,它很簡單Ÿ有用的例子。最好的問候。 – Alexander

+0

太棒了!請注意,如果您希望即使其他UI元素正在進行(例如UIScrolLView),此動畫也會繼續更新,您需要使用NSRunLoopCommonModes安排定期處理。 – 2013-07-29 02:22:53

0

通常核心動畫是最好的方法。退房the documentation

+0

我認爲不是。如果他真的想在標籤從文本變爲空白或回到['animateWithDuration']時進行動畫過渡(http://developer.apple.com/library/ios/documentation/uikit/reference/uiview_class/UIView/UIView。 html#// apple_ref/doc/uid/TP40006816-CH3-SW109)會容易得多。但我認爲這不是問題。 – Rob

+0

好的,什麼是可以事件/調度的開始顯示(對於NNString單詞:arrayWords)當新頁面將顯示?沒有用戶交互..感謝 – Alexander