views:

26

answers:

0

I am having an issue where I add a transport control bar to a UIView to provide control for an audio player, and the buttons don't show up all the time. If I tap them (or rather, tap where they should be), they suddenly appear. To complicate matters, most of the time they show up fine...this is kind of sporadic.

Could this be a memory issue of some sort? Leaks finds nothing, and the activity monitor doesn't seem to be choking on anything.

Here are functions that adds the overlay in question. It's a bit nutty due to the "back 30 seconds" functionality. It's basically a guided interactive tour that scrolls around a map based on the time of an audio cue, adding the overlay at specific times. The view "transport" contains two buttons; a play/pause button, and a back 30 button. These are the buttons that occasionally fail to show.

checkPlayerTime decides if something should happen based on the audio time code, and adds the transport as needed. performZoomsForWaypointAndHotspot zooms into the scrollview and adds an overlay with information. undoZoomsAndPrepareForNextHotspotForWaypointAndHotspot zooms out and removes the overlay. playPause and backTHirty are linked to the transport buttons.

Sorry if the section of code is a bit convoluted/out of context. I'm happy to add more code to put it in context.

- (void)checkPlayerTime:(int)sender
{
    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
    WaypointModel *currentWaypoint = [[sharedValues waypoints]objectAtIndex:sender];
    int hotspotIndex = [[currentWaypoint hotspots] count];
    Boolean *hasBeenPlayed = malloc(hotspotIndex * sizeof(Boolean));
    Boolean *hasZoomedOut = malloc(hotspotIndex * sizeof(Boolean));
    double earlierTime;
    for (int z=0; z < hotspotIndex; z++) {
        hasBeenPlayed[z] = FALSE;
        hasZoomedOut[z] = FALSE;
    }
    debugLog(@"There are a total of %i hotspots coming up.", hotspotIndex);
    if (![newPlayer isPlaying]) {
    } else {
        [[[UIApplication sharedApplication] keyWindow] addSubview:transport.view];

    }
    while ([newPlayer isPlaying] || (![newPlayer isPlaying] && ([newPlayer currentTime]!=0.0))) {
        earlierTime = [newPlayer currentTime];

        for (int x=0; x<hotspotIndex; x++) {
            Hotspot *currentHotspot = [[currentWaypoint hotspots]objectAtIndex:x];
            NSMutableArray *indexPackage = [[NSMutableArray alloc] init];
            [indexPackage addObject:[NSNumber numberWithInt:sender]];
            [indexPackage addObject:[NSNumber numberWithInt:x]];

            if (([newPlayer currentTime] >= currentHotspot->cueTime) && !hasBeenPlayed[x]) {
                hasBeenPlayed[x] = TRUE;
                [self performSelectorOnMainThread:@selector(performZoomsForWaypointAndHotspot:) withObject:indexPackage waitUntilDone:FALSE];
            } if (([newPlayer currentTime] >= currentHotspot->cueTime + currentHotspot->cueDuration) && !hasZoomedOut[x]) {
                hasZoomedOut[x] = TRUE;
                [self performSelectorOnMainThread:@selector(undoZoomsAndPrepareForNextHotspotForWaypointAndHotspot:) withObject:indexPackage waitUntilDone:FALSE];
            }
            currentHotspot = nil;
            [indexPackage autorelease];
            indexPackage = nil;
            debugLog(@"earlier time is now %f, current time is now %f", earlierTime, [newPlayer currentTime]);
            if (earlierTime > [newPlayer currentTime]) {
                for (int q=hotspotIndex-1; q>=0; q--) {
                    Hotspot *pauseCheck = [[currentWaypoint hotspots]objectAtIndex:q];
                    if (pauseCheck->cueTime > [newPlayer currentTime]) {
                        hasBeenPlayed[q] = FALSE;
                        hasZoomedOut[q] = FALSE;
                    }
                }
            }
        }

    }
    if (![newPlayer isPlaying]) {
        isWaypointAllowed = TRUE;
        if ([newPlayer currentTime] == 0.0) {
            [transport.view removeFromSuperview];
        }
        [[waypoints objectAtIndex:sender] setImage:[UIImage imageNamed:[NSString stringWithFormat:@"w%i.png", sender+1]] forState:UIControlStateNormal];

    }
    free(hasBeenPlayed);
    free(hasZoomedOut);
    [pool release];
}

- (void)performZoomsForWaypointAndHotspot:(id)sender
{
    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
    int hotspot = [[sender objectAtIndex:1] intValue];
    int waypoint = [[sender objectAtIndex:0] intValue];
    WaypointModel *currentWaypoint = [[sharedValues waypoints] objectAtIndex:waypoint];
    Hotspot *currentHotspot = [[currentWaypoint hotspots] objectAtIndex:hotspot];
    CGRect zoomRect = CGRectMake(currentHotspot->worldLocation.x-120, currentHotspot->worldLocation.y-80, 240, 160);
    [scrollView zoomToRect:zoomRect animated:YES];
    readyForZoomIn = TRUE;


    CGAffineTransform transform = CGAffineTransformMakeRotation(M_PI/2);
    tempOverlay.view.transform = transform;

    // Repositions and resizes the view.
    CGRect contentRect = CGRectMake(-107, -80, 480, 320);
    tempOverlay.view.bounds = contentRect;

    [[[UIApplication sharedApplication] keyWindow] addSubview:tempOverlay.view];

    scrollView.scrollEnabled = NO;
    [[tempOverlay textView] setText:[[[sharedValues interactionPoints]objectAtIndex:hotspot] text]];
    [[tempOverlay label] setText:[[[sharedValues interactionPoints]objectAtIndex:hotspot] title]];
    [[tempOverlay imageView] setImage:[UIImage imageNamed:[[[sharedValues interactionPoints]objectAtIndex:hotspot] image]]];

    [pool release];

}

- (void)undoZoomsAndPrepareForNextHotspotForWaypointAndHotspot:(id)sender
{
    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
    int hotspot = [[sender objectAtIndex:1] intValue];
    int waypoint = [[sender objectAtIndex:0] intValue];
    debugLog(@"This should undo zoom for waypoint %i, hotspot %i", waypoint, hotspot);
    WaypointModel *currentWaypoint = [[sharedValues waypoints] objectAtIndex:waypoint];
    Hotspot *currentHotspot = [[currentWaypoint hotspots] objectAtIndex:hotspot];
    CGRect zoomRect = CGRectMake(currentHotspot->worldLocation.x-120, currentHotspot->worldLocation.y-80, 480/((float)(2/3)), 320/((float)(2/3)));
    [scrollView zoomToRect:zoomRect animated:YES];
    freshCue = TRUE;
    readyForZoomOut = TRUE;

    [tempOverlay.view removeFromSuperview];
    scrollView.scrollEnabled = YES;

    [pool release];
}

- (void)playPause:(id)sender
{
    if ([newPlayer isPlaying]) {
        [newPlayer pause];
    } else {
        [newPlayer play];
    }
}

- (void)backThirty:(id)sender
{
    if ([newPlayer isPlaying]) {
        if ([newPlayer currentTime] > 30) {
            [newPlayer setCurrentTime:[newPlayer currentTime] - 30.0];
        } else {
            [newPlayer setCurrentTime:0.0];
        }

   }
}