2011-08-19 75 views
0

我正在使用此代码在屏幕上显示随机图像。Xcode删除触摸事件图像?

- (void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { 

    UITouch *touch = [[event allTouches] anyObject]; 
    CGPoint location = [touch locationInView:touch.view]; 

    UIImageView *cloud = [[UIImageView alloc] 
           initWithImage:[UIImage imageNamed:@"spit.png"]]; 

    cloud.center = CGPointMake(random() % 250, random() % 400); //Random place 
    [cloud setAlpha:1]; 
    [self.view addSubview:cloud]; 
    [cloud release]; 

    cloud.frame = CGRectMake(0, 0, 300, 300); 

    [UIView beginAnimations: @"cloddy" context: nil]; 
    [UIView setAnimationDuration: 0.5f]; 
    [UIView setAnimationCurve: UIViewAnimationCurveEaseIn]; 

    // Size when done 
    cloud.frame = CGRectMake(0, 0, 75, 75); 
    cloud.center = location; 
    [UIView commitAnimations]; 
} 

经过几次触摸屏幕变得满满。我如何在另一个IBAction中删除它们?为什么动画总是从左上角开始,即使我正在使用随机?

回答

1

假设已添加到您的视图的唯一子视图是clouds

- (IBAction)clearClouds:(id)sender{ 
    [[[self view] subviews] makeObjectsPerformSelector: @selector(removeFromSuperview)]; 
} 

云总是开始于左上角,因为这条线:cloud.frame = CGRectMake(0, 0, 300, 300);它的起源重置为0,0。将cloud.center = CGPointMake(random() % 250, random() % 400);移动到您设置云框架的线下方,并且应该全部设置。