我想为我的标签使用一点动画。如果我尝试从 IBAction 按钮或 ViewDidLoad 调用此动画并且一切正常。
- (void)animateLabelShowText:(NSString*)newText characterDelay:(NSTimeInterval)delay
{
[self.myLabel setText:@""];
for (int i=0; i<newText.length; i++)
{
dispatch_async(dispatch_get_main_queue(),
^{
[self.myLabel setText:[NSString stringWithFormat:@"%@%C", self.myLabel.text, [newText characterAtIndex:i]]];
});
[NSThread sleepForTimeInterval:delay];
}
}
调用者:
-(void) doStuff
{
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_LOW, 0),
^{
[self animateLabelShowText:@"Hello Vignesh Kumar!" characterDelay:0.5];
});
}
但是,如果我将此方法放在协议中并尝试从例如委托中调用它,则不会出现任何内容。我可能在 GDC(Grand Central Dispatch)逻辑中遗漏了一些东西:
...
if ([_myDelegate respondsToSelector:@selector(doStuff:)])
{
NSLog(@" Yes I'm in and try to execute doStuff..");
[_myDelegate doStuff]; // NOTHING TO DO
}
...
当我用类似的函数改变我的函数 doStuff 时,同样的情况发生了:
-(void)typingLabel:(NSTimer*)theTimer
{
NSString *theString = [theTimer.userInfo objectForKey:@"string"];
int currentCount = [[theTimer.userInfo objectForKey:@"currentCount"] intValue];
currentCount ++;
NSLog(@"%@", [theString substringToIndex:currentCount]);
[theTimer.userInfo setObject:[NSNumber numberWithInt:currentCount] forKey:@"currentCount"];
if (currentCount > theString.length-1) {
[theTimer invalidate];
}
[self.myLabel setText:[theString substringToIndex:currentCount]];
}
由
-(void) doStuff
{
NSString *string =@"Risa Kasumi & Yuma Asami";
NSMutableDictionary *dict = [NSMutableDictionary dictionary];
[dict setObject:string forKey:@"string"];
[dict setObject:@0 forKey:@"currentCount"];
NSTimer *timer = [NSTimer scheduledTimerWithTimeInterval:0.1 target:self selector:@selector(typingLabel:) userInfo:dict repeats:YES];
[timer fire];
}