在没有名称冲突的情况下产生无限的UIImageViews

时间:2010-11-17 17:53:18

标签: iphone uiimageview spawning

iPad应用。 OS 4.2。

我有一个按钮,当按下该按钮时,会调用此函数创建一个UIImageView并对其进行动画处理(并调用第二个动画块)。我创建了另一个调用相同功能的按钮,并将不同的位置和不同的URL传递给图形。

我开始出现故障 - 从第一个按钮产生的第一个图形具有从第二个按钮传递的位置。这可能是因为我没有动态命名UIImageView并从中获取冲突。

那么如何动态创建和命名任何无限数量的UIImageViews?特别是因为要在两个函数中引用UIImageView,它需要在函数之外声明。

    -(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];
}

1 个答案:

答案 0 :(得分:1)

你有一个指向testWord的指针而不是每个实例的指针。您应该使用动画的上下文属性来传递您想要消失的特定UIImageView

-(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];
}