Search code examples
objective-cuilabelhighlighting

Sequential Highlighting of a UILabels with sound clips


I have been searching for a answer for this issue for a while now, and haven't had any luck.

Here is the issue: I have four UILabels, and sound clips to go with them. I want each label to show highlighted text when it's sound clip is played.

The problem is that only the last label is gets highlighted when I test it on simulator.

Here are some things that I've tried:

[self playSound:@"this"];
[thisLabel setTextColor:[UIColor orangeColor]];
sleep(1);
[self playSound:@"is"];
[thisLabel setTextColor:[UIColor blackColor]];
[isLabel setTextColor:[UIColor orangeColor]];
[self playSound:@"a"];
[isLabel setTextColor:[UIColor blackColor]];
[aLabel setTextColor:[UIColor orangeColor]];
[self playSound:@"word"];
[aLabel setTextColor:[UIColor blackColor]];
[currentWord setTextColor:[UIColor orangeColor]];

This is when I discovered that only the last label would show up highlighted when I tested it in the simulator.

The next thing I tried is putting a call to this function at the end of t he playSound function.

- (void)shiftHighlight:(NSString *)word
{
    UILabel *currentWord = [self currentWord];
    UILabel *thisLabel = [self thisLabel];
    UILabel *isLabel = [self isLabel];
    UILabel *aLabel = [self aLabel];
    if ([word isEqualToString:@"this"])
    {
        [thisLabel setHighlighted:YES];

        [isLabel setHighlighted:NO];
        [aLabel setHighlighted:NO];
        [currentWord setHighlighted:NO];
    } else if ([word isEqualToString:@"is"]) {
        [isLabel setHighlighted:YES];

        [thisLabel setHighlighted:NO];
        [aLabel setHighlighted:NO];
        [currentWord setHighlighted:NO];
    } else if ([word isEqualToString:@"a"]) {
        [aLabel setHighlighted:YES];

        [thisLabel setHighlighted:NO];
        [isLabel setHighlighted:NO];
        [currentWord setHighlighted:NO];
    } else {
        [currentWord setHighlighted:YES];

        [thisLabel setHighlighted:NO];
        [isLabel setHighlighted:NO];
        [aLabel setHighlighted:NO];
    }
}

With the color of the highlighted text set as orange elsewhere. This had the same effect as the first attempt.

Most recently I tried putting successive calls to this function:

- (void)readSection:(NSString *)section
{
    [self playSound:section];
    [self shiftHighlight:section];
}

I've also tried something with NSTimers, but that got very confusing and it seemed like I was barking up the wrong tree with them.

Behavior is enabled on all labels.

So, if anyone has a solution for me, or can carefully explain how to use NSTimers for this issue, I'd her very thankful.


Solution

  • In all cases the statements are not waiting for the sound to finish playing and gets executed before-hand in the order of occurrence. The last statement executed will be the final result, i.e the final label being highlighted.
    If your playSound method has a way to pass a call-back block or a delegate function that gets executed after completion that is the place where you should be writing the logic to the highlight label or play the next sound.