2013-06-28 33 views
0

我想为我的标签使用一点动画。 如果我尝试从IBAction按钮或ViewDidLoad调用此动画,并且所有工作都正常。由代理/协议结构调用的UILabels动画

- (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(大中央调度)逻辑:

... 
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 [email protected]"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]; 
} 

回答

0

我已经解决了在我的代理文件中使用NSNotificationCenter的问题.m

viewDidLoad中:

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(makeEffectOnMyLabel:) name:@"lblUpdate" object:nil]; 

我的协议功能:

dispatch_async(dispatch_get_main_queue(), 
    ^{ 
     ...I've populated a dictionary userInfo with my vars 
     [[NSNotificationCenter defaultCenter] postNotificationName:@"lblUpdate" object:nil userInfo:userInfo]; 
    }); 

函数创建准备typingLabel

-(void) makeEffectOnMyLabel:(NSNotification *) notification 
{ 
    ..changes between dictionaries to prepare typingLabel.. 

    NSMutableDictionary *dict = [NSMutableDictionary dictionary]; 
    NSDictionary* userInfo = [notification userInfo]; 

    [dict setObject:[userInfo objectForKey:@"myRes"] forKey:@"myRes"]; 
    [dict setObject:[userInfo objectForKey:@"myType"] forKey:@"myType"]; 
    [dict setObject:[userInfo objectForKey:@"frase"] forKey:@"frase"]; 
    [dict setObject:@0 forKey:@"currentCount"]; 
    NSTimer *timer = [NSTimer scheduledTimerWithTimeInterval:0.1 target:self selector:@selector(typingLabel:) userInfo:dict repeats:YES]; 
    [timer fire]; 
} 

现在,所有的工作是正确的。