【问题标题】:Spawning infinite UIImageViews without name collisions产生无限的 UIImageViews 没有名称冲突
【发布时间】:2010-11-17 17:53:18
【问题描述】:

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

【问题讨论】:

    标签: iphone uiimageview spawning


    【解决方案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];
    }
    

    【讨论】:

      猜你喜欢
      • 2014-11-12
      • 2021-06-03
      • 2021-01-17
      • 2016-05-15
      • 2017-10-31
      • 2023-02-10
      • 1970-01-01
      • 2018-04-11
      • 2021-04-16
      相关资源
      最近更新 更多