There is a UIImageView view with a picture inside (coin picture) created in the storyboard. I press a button on top of this view, start IBAction, which rotates this coin, creates a view on top of it using addSubview (a series of pictures, animation), stops one of the sides. Everything is fine here, but if you press the button that starts the rotation again, the coin rotates, and you can see from it the coin with the coin that fell before. How to remove it?
Here are two basic methods:
First method
-(IBAction)pushButton:(id)sender { self.tailimage.hidden = YES; // вот тут убираю картинку с монеткой, изначально созданную в storyboard, на первом нажатии все хорошо работает, на втором остается NSArray *images = [NSArray arrayWithObjects: [UIImage imageNamed:@"heads.png"], [UIImage imageNamed:@"heads2.png"], [UIImage imageNamed:@"heads3.png"], [UIImage imageNamed:@"heads4.png"], [UIImage imageNamed:@"heads5.png"], [UIImage imageNamed:@"heads6.png"], [UIImage imageNamed:@"heads7.png"], [UIImage imageNamed:@"heads8.png"], [UIImage imageNamed:@"heads9.png"], [UIImage imageNamed:@"heads10.png"], [UIImage imageNamed:@"heads11.png"], [UIImage imageNamed:@"heads12.png"], [UIImage imageNamed:@"heads13.png"], [UIImage imageNamed:@"heads14.png"], [UIImage imageNamed:@"heads15.png"], [UIImage imageNamed:@"heads16.png"], [UIImage imageNamed:@"heads17.png"], [UIImage imageNamed:@"heads18.png"], [UIImage imageNamed:@"heads19.png"], [UIImage imageNamed:@"heads20.png"], [UIImage imageNamed:@"tails.png"], nil]; UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(51, 234, 210, 210)]; imageView.animationImages = images; imageView.animationDuration = 0.50; imageView.animationRepeatCount = 40; [self.view addSubview:imageView]; [imageView startAnimating]; [self performSelector:@selector(randomize:) withObject:nil afterDelay:2.0]; } Second method
-(IBAction)randomize:(id)sender { UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(51, 234, 210, 210)]; int randomimages = rand() % 2; switch (randomimages) { case 0: imageView.image = [UIImage imageNamed:@"heads.png"]; break; case 1: imageView.image = [UIImage imageNamed:@"heads11.png"]; break; } [self.view addSubview:imageView]; }