Resolved Image views not hiding

Discussion in 'iOS Programming' started by moonman239, Nov 6, 2014.

  1. moonman239, Nov 6, 2014
    Last edited: Nov 11, 2014

    moonman239 macrumors 68000

    Joined:
    Mar 27, 2009
    #1
    The following method creates a burst effect and plays an accompanying sound. The problem is that each smaller star image view is supposed to hide, as indicated in one of the lines of code, but none of them do. Edit: Thanks to the NSLog command in that code, I can see that each relevant image view's "hidden" property is set to YES.

    Here's the method:

    Code:
    -(void)playBurstingStarAnimation
    {
        [[self stillStarImageView] setHidden:false];
        float radius = 250;
        __block NSInteger animsCompleted = 0;
        for (UIImageView *starImageView in [self animatedStarImageViews])
        {
            [starImageView setHidden:false];
            CGPoint stillStarCenter = [[self stillStarImageView] center];
            CGPoint starCenter = [starImageView center];
            CGPoint differencePoint = CGPointMake((starCenter.x - stillStarCenter.x), (starCenter.y - stillStarCenter.y));
            CGFloat angle = atan2f(differencePoint.y, differencePoint.x);
            CGFloat destinationX = (cosf(angle) * radius) + (stillStarCenter.x);
            CGFloat destinationY = (sinf(angle) * radius) + (stillStarCenter.y);
            CGPoint destination = CGPointMake(destinationX, destinationY);
            [UIView animateWithDuration:3 animations:^{ [starImageView setCenter:destination]; } completion:^(BOOL finished){ animsCompleted = animsCompleted + 1; if (animsCompleted == [[self animatedStarImageViews] count]) { [starImageView setHidden:true]; NSLog(@"%@", [starImageView description]); [self performSelector:starAnimationCompleted]; }; }];
        }
        NSURL *burstSound = [[NSBundle mainBundle] URLForResource:@"burst" withExtension:@"mp3"];
        AVPlayerItem *burstSoundItem = [AVPlayerItem playerItemWithURL:burstSound];
        [[self queuePlayer] insertItem:burstSoundItem afterItem:nil];
        [[self queuePlayer] play];
    }
    
    Please help me, and thanks!
     
  2. moonman239 thread starter macrumors 68000

    Joined:
    Mar 27, 2009
    #2
    It seems I need to get the setHidden method to be invoked on the main thread. How do I do that?

    I tried
    Code:
    performSelectorOnMainThread: withObject:@YES
    but that did not work.
     
  3. moonman239 thread starter macrumors 68000

    Joined:
    Mar 27, 2009
    #3
    Other than having a selector performed on the main thread every time the completion block executes, I don't know what else to do. Any ideas?

    Edit: I should add that I made some changes, and now that performSelector method is only called when animationCount = the number of UIViews (or, at least, that's how it's supposed to work.)
     
  4. moonman239, Nov 11, 2014
    Last edited: Nov 11, 2014

    moonman239 thread starter macrumors 68000

    Joined:
    Mar 27, 2009
    #4
    Update: I have an integer that is supposed to be set to he number of animated image views. I had another integer that was supposed to be set to the number of image views that already performed an animation. I think that the problem is that they are always equal, due in part to iOS's handling of integers that are defined outside a block.

    The solution, therefore, seems to be to append
    Code:
    __block
    to the beginning of the line in which I declare the first integer. I will see if doing so solves the problem.

    Edit: Looks like I put an if line in the wrong place. So I did as I said I would do, and I put the "if" line in the right place, and voila! Now the code works!
     

Share This Page