Note that there are some explanatory texts on larger screens.

plurals
  1. POMaking a smoothly animated entrance, exit, and swapping of two image views
    primarykey
    data
    text
    <p>I am using UIView's animation features to swim one image of a fish onto the screen and when there already exists a fish in the middle of the screen, swim it off and swim a new one on. The selection of the fish is sent from a separate tableview controller. </p> <p>To accomplish this, I m keeping two instance variables of UIImageView and exchanging self.fishViewCurrent for self.fishViewNext once the animation is completed (see code below).</p> <p>My problem is when the user presses on new table cells quickly, before the next fish has had a chance to arrive. My quick solution is to delay a new message that will add the fish and just do nothing at the moment. This works for 2 quick touches, but not more. I am looking for ideas if anyone knows how I can improve the code below, so that the animation is smooth and you can click on whatever while the final displayed image is the one last clicked. Here re the important methods:</p> <p>Creating the first image view:</p> <pre><code>- (void) createFish { self.fishViewCurrent = [[[UIImageView alloc] init] autorelease]; self.fishViewCurrent.contentMode = UIViewContentModeScaleAspectFit; self.fishViewCurrent.bounds = CGRectMake(0.0f, 0.0f, kFishWidth, kFishHeight); self.fishViewCurrent.center = CGPointMake(-kFishWidth, kFishYPos); // off the screen to the left [self addSubview:self.fishViewCurrent]; } </code></pre> <p>Public method called when a tableview cell is selected:</p> <pre><code>- (void) addFish:(UIImage *)fish { if (self.fishViewNext) { [self performSelector:@selector(addFish:) withObject:(UIImage *)fish afterDelay:1.0]; return; } self.fishViewNext = [[[UIImageView alloc] initWithImage:fish] autorelease]; self.fishViewNext.contentMode = UIViewContentModeScaleAspectFit; self.fishViewNext.bounds = CGRectMake(0.0f, 0.0f, kFishWidth, kFishHeight); self.fishViewNext.center = CGPointMake(self.bounds.size.width + kFishWidth, kFishYPos); // off the screen right [self addSubview:self.fishViewNext]; [UIView beginAnimations:@"swimFish" context:nil]; [UIView setAnimationDuration:2.0f]; [UIView setAnimationCurve:UIViewAnimationCurveEaseInOut]; [UIView setAnimationBeginsFromCurrentState:YES]; [UIView setAnimationDelegate:self]; [UIView setAnimationDidStopSelector:@selector(fishSwamOffHandler:finished:context:)]; [UIView setAnimationRepeatAutoreverses:NO]; self.fishViewNext.center = CGPointMake(self.center.x, kFishYPos); // center self.fishViewCurrent.center = CGPointMake(-kFishWidth, kFishYPos); // exit stage left [UIView commitAnimations]; } </code></pre> <p>Handler to switch the 'next' fish (UIImageView) to the 'current':</p> <pre><code>- (void)fishSwamOffHandler:(NSString *)animationID finished:(NSNumber *)finished context:(void *)context { [self.fishViewCurrent removeFromSuperview]; self.fishViewCurrent = self.fishViewNext; self.fishViewNext = nil; } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload