2011-08-19 84 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);移動到您設置雲框架的線下方,並且應該全部設置。