0

iPad 应用程序。操作系统 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];
}
4

1 回答 1

1

您有一个指向 testWord 的指针,而不是每个实例的指针。您应该使用动画的 context 属性来传递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];
}
于 2010-11-17T18:03:09.927 回答