2010-11-17 107 views
0

iPad应用程序。 OS 4.2。产生无名UIImageViews无名称冲突

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

我开始出现毛刺 - 从第一个按钮衍生的第一个图形具有从第二个按钮传递的位置。这可能是由于我没有动态命名UIImageView并从中获得碰撞。

那么,我该如何动态创建和命名任何无限数量的UIImageView?特别是因为要在两个函数中引用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

您有一个指向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]; 
}