0

我想为我的标签使用一点动画。如果我尝试从 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];
}
4

1 回答 1

0

我已经在我的委托文件 .m 中使用 NSNotificationCenter 解决了

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];
}

现在一切正常。

于 2013-07-02T08:44:13.720 回答