2015-01-09 33 views
2

我有一個字符串塊,並且我將所有字符串放在數組列表中,用空格分隔,並且想要在同一個文本框中逐一顯示,假設我有一個字符串「我的名字是某某」,然後文本框應主張這樣的「我」,然後等待幾秒鐘,然後顯示「名稱」,並再次等待和顯示數據「是」等等.....從iOS中的數組列表中逐個顯示字符串Xcode 5

- (IBAction)btnstart:(id)sender { 

    NSString *myString=self.textcollector.text; 
    NSArray *readerArray=[myString componentsSeparatedByString:@" "]; 
    int arraylenght=[readerArray count]; 
    [email protected]""; 


    for(int i=0;i<arraylenght;i++) 
    { 


    //textcollector=NULL; 
    self.textcollector.text=readerArray[i]; 
    //NSLog(@" ",readerArray[i]); 
    sleep(1); 
    // textcollector.a 

    } 

}

+0

試試這個,宣佈readerArray我作爲全球

NSArray *readerArray; int i; - (IBAction)btnstart:(id)sender { NSString *myString=self.textcollector.text; readerArray=[myString componentsSeparatedByString:@" "]; NSUInteger arraylenght=[readerArray count]; [email protected]""; [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(showText) userInfo:nil repeats:YES]; } -(void)showText { if (i<[readerArray count]) { self.textcollector.text=readerArray[i]; i++; } } 

無效計時器不使用的睡眠。看看NSTimer – Paulw11

+0

[打字機效果文本動畫]可能的重複(http://stackoverflow.com/questions/27736360/typewriter-effect-text-animation) – Paulw11

+0

看到我下面的答案。 –

回答

1

看到我的代碼:

在您的.h file:定義字符串數組。

@property (strong, nonatomic) NSArray *aryAll; 

在您的.m file

@synthesize aryAll; 

在動畫btnstart

- (IBAction)btnstart:(id)sender { 

     self.aryAll = [[NSArray alloc] init]; 

     NSString *stringAnimation = @"My name is kirit modi come from deesa city"; 

     aryAll = [stringAnimation componentsSeparatedByString:@" "]; 

     dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_LOW, 0), 
         ^{ 
          [self animationString]; 
         }); 

    } 

方法:

-(void)animationString 
{ 
    for (int i =0; i< aryAll.count; i++) 
    { 
     dispatch_async(dispatch_get_main_queue(), 
         ^{ 
          [labnanme setText:[aryAll objectAtIndex:i]]; 
         }); 

     [NSThread sleepForTimeInterval:1]; 
    } 
} 

您輸出爲:

enter image description here

+0

感謝您的優秀回覆,但我想顯示按鈕點擊數據.. – shashi

+0

是我更新代碼,當你點擊按鈕。 –

+0

謝謝先生。莫迪...你搖滾....這是非常好的..非常感謝你 – shashi

-1

最後

+0

「宣佈讀者陣列和我作爲全球」 - ***沒有神,***請不要。沒有必要也沒有理由。 –

相關問題