views:

25

answers:

1

I have this code for flashing an image on a map as a part of MKAnnotationView:

    UIView* containerView = [mapView viewForAnnotation:enemy];
    UIImageView* redCircle = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"Red_half_circle.png"]];


    [UIView transitionWithView:containerView
                      duration:2.9
                       options:UIViewAnimationOptionAutoreverse&&UIViewAnimationOptionRepeat
                    animations:^{ [containerView addSubview:redCircle];}
                    completion:^(BOOL finished){[redCircle removeFromSuperview]; }];

As such, nothing happens when I run it. But!!! If I remove the "completion" code block and replace it with NULL, then at least the animation adds the subview. Maybe the options have something to do with it?

Question: How do you flash an image in MKAnnotationView?

Thanks much.

+1  A: 

First things first:

You've specified UIViewAnimationOptionAutoreverse&&UIViewAnimationOptionRepeat. This is a "logical AND", not a "bitwise AND". Autoreverse is 16 (1<<4) and Repeat is 8 (1<<3), so Autoreverse&&Repeat is 16&&8 which is the same as 1 (since they're evaluated as truth values), which happens to be the same as UIViewAnimationOptionLayoutSubviews.

You want UIViewAnimationOptionAutoreverse&UIViewAnimationOptionRepeat.

Second: You haven't specified a transition; this is equivalent to specifying UIViewAnimationOptionTransitionNone. I think this means that the duration is ignored (because you've specified TransitionNone), so completion happens immediately, so you don't see a thing.

Third: I also don't think that UIView transitions can be repeated/reversed (it would need to keep track of the views you've removed/added; AFAIK it only keeps a "screenshot" of the initial state), but ICBW.

You may have better luck with something like this:

[containerView addSubview:redCircle];
redCircle.alpha = 0;
[UIView animateWithDuration:2.9
  delay:0
  options:UIViewAnimationOptionAutoreverse&UIViewAnimationOptionRepeat
  animation:^{redCircle.alpha = 1;}
  completion:NULL];

This should make it fade in and out. You might be able to make it flash by setting redCircle.hidden instead (UIView.hidden is not documented as animatable, but CALayer.hidden is animatable, so it might still work).

tc.
Though anecdotal, it's definitely my experience that the hidden property is animatable.
zpasternack
Does it change hidden-ness half-way through the animation?
tc.