Reputation: 2415
I'm trying to create a typewriter effect. my code works fine. My problem, and this ... UITextView fails to update in real time. this is my code:
NSMutableArray *testochar=[util getTestoChar:value];
for (int i=0; i<[testochar count]; i++){
NSString *y=[testochar objectAtIndex:i];
[NSThread sleepForTimeInterval:0.1f];
self.txtview.text=[self.txtview.text stringByAppendingString:y];
}
Upvotes: 0
Views: 292
Reputation: 396
All modification on UI you should do in main thread, try use performSelectorOnMainThread:withObject:waitUntilDone:
call
//...
for(int i=0;i<[testochar count];i++){
NSString *y=[testochar objectAtIndex:i];
[NSThread sleepForTimeInterval:0.1f];
NSDictionary *arg = [NSDictionary dictionaryWithObjectAndKeys:
self.txtview, @"textView",
y, @"string", nil ];
[self performSelectorOnMainThread:@selector(updateTextForTextView:) withObject:arg waitUntilDone:NO];
}
//.....
-(void)updateTextForTextView:(NSDictionary*)arg {
NSString *string = [arg objectForKey:@"string"];
UITextView *textView = [arg objectForKey:@"textView"];
self.txtview.text=[self.txtview.text stringByAppendingString:string];
}
(update)
try
- (void)viewDidLoad {
[super viewDidLoad];
textView.text = @"";
[[NSTimer scheduledTimerWithTimeInterval:0.1 target:self selector:@selector(update) userInfo:nil repeats:YES] retain];
}
-(void) update {
static char text[] = "HELLO";
static int i =0;
if (text[i%6]==0) {
textView.text = [NSString stringWithFormat:@""];
} else {
textView.text = [NSString stringWithFormat:@"%@%c", textView.text, text[i%6] ];
}
i++;
}
it works like: http://www.youtube.com/watch?v=tB2YKX4zpY4
Upvotes: 3
Reputation: 2415
really thanks for your advice. I did it by modifying the code this way.
1) self.timer = [[NSTimer scheduledTimerWithTimeInterval:0.1 target:self selector:@selector(update) userInfo:nil repeats:YES] retain];
-(void) update {
char text[[self.testo length]];
for(int i=0;i<self.testo.length;i++){
text[i]=[self.testo characterAtIndex:i];
}
static int i =0;
if (i==self.testo.length) {
/// txtview.text = [NSString stringWithFormat:@""];
[self.timer invalidate];
} else {
txtview.text = [NSString stringWithFormat:@"%@%c", txtview.text, text[i%self.testo.length] ];
}
i++;
}
Upvotes: 2