Question

iPad app. OS 4.2.

I have a button that, when pressed, calls this function that creates a UIImageView and animates it (and calls a second animation block). I created another button that calls the same function and passes different locations and different URL to a graphic.

I'm starting to get glitches—the first graphic spawned from the first button has the locations passed from the second button. This is probably due to the fact that I am not dynamically naming the UIImageView and getting collisions from that.

So how do I dynamically create and name any infinite number of UIImageViews? Especially since, to reference the UIImageView in two functions, it needs to be declared outside of the functions.

    -(void)makeSoundEffectWordAppear:(NSString *)imageName:(int)startingX:(int)startingY:(int)endingX:(int)endingY{
        myImage = [UIImage imageNamed:imageName];
        [testWord setImage:myImage];
        testWord = [[[UIImageView alloc] initWithFrame:CGRectMake(startingX,startingY,myImage.size.width,myImage.size.height)] autorelease];    
        [self.view addSubview:testWord];
        testWord.alpha=0;
        testWord.transform = CGAffineTransformMakeScale(.5, .5);

        [UIView beginAnimations:@"moveWord" context:nil];
            [UIView setAnimationDelegate:self];
            [UIView setAnimationDidStopSelector:@selector(fadeWord:finished:context:)];
            //[UIView setAnimationCurve:UIViewAnimationCurveEaseIn];
            [UIView setAnimationDuration:1];

            [UIView setAnimationCurve:UIViewAnimationCurveEaseIn];
            [UIView setAnimationBeginsFromCurrentState:YES];
            testWord.transform = CGAffineTransformMakeScale(1, 1);
            testWord.center = CGPointMake(endingX,endingY);
                        testWord.alpha=1;
        [UIView commitAnimations];
    }

- (void)fadeWord:(NSString *)animationID finished:(BOOL)finished context:(void *)context{
        [UIView beginAnimations:@"makeWordFade" context:nil];
    [UIView setAnimationDelegate:self];
        [UIView setAnimationDelay:5];
        testWord.alpha=0;
        testWord.transform = CGAffineTransformMakeScale(.5, .5);
        [UIView setAnimationDuration:1];
    [UIView commitAnimations];
}
Was it helpful?

Solution

You have a single pointer to testWord rather than a pointer for each instance. You should be using the context property of animations to pass in the specific UIImageView you want to fade away:

-(void)makeSoundEffectWordAppear:(NSString *)imageName:(int)startingX:(int)startingY:(int)endingX:(int)endingY{
    UIImage *myImage = [UIImage imageNamed:imageName];
    UIImageView *testWord = [[[UIImageView alloc] initWithImage:myImage] autorelease];
    CGRect frame = [testWord frame];
    frame.origin.x = startingX;
    frame.origin.y = startingY;
    [testWord setFrame:frame];
    [self.view addSubview:testWord];
    testWord.alpha=0;
    testWord.transform = CGAffineTransformMakeScale(.5, .5);

    [UIView beginAnimations:@"moveWord" context:testWord];
        [UIView setAnimationDelegate:self];
        [UIView setAnimationDidStopSelector:@selector(fadeWord:finished:context:)];
        //[UIView setAnimationCurve:UIViewAnimationCurveEaseIn];
        [UIView setAnimationDuration:1];

        [UIView setAnimationCurve:UIViewAnimationCurveEaseIn];
        [UIView setAnimationBeginsFromCurrentState:YES];
        testWord.transform = CGAffineTransformMakeScale(1, 1);
        testWord.center = CGPointMake(endingX,endingY);
                    testWord.alpha=1;
    [UIView commitAnimations];
}

- (void)fadeWord:(NSString *)animationID finished:(BOOL)finished context:(void *)context{
    UIImageView *testWord = (UIImageView *)context;
    [UIView beginAnimations:@"makeWordFade" context:context];
        [UIView setAnimationDelegate:self];
        [UIView setAnimationDelay:5];
        testWord.alpha=0;
        testWord.transform = CGAffineTransformMakeScale(.5, .5);
        [UIView setAnimationDuration:1];
    [UIView commitAnimations];
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top