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

    1 answer 1

    Open the secret, but why do you need three (with self.tailimage) pictures, if at any given moment only one of them is shown on the screen, and they, apparently, are of the same size? It took an animation - they inserted the array into self.tailimage.animationImages and launched it, someone pressed the (- (IBAction) randomize: (id) sender button, if it's not a button, then IBAtion is not needed, and without a sender it will work anyway) - set the desired image in self.tailimage.image, stopped the animation, made self.tailimage.animationImages = nil.

    In general, to remove, you need to remove the removeFromSuperview method from the UIImageView, only you didn’t leave a pointer to your pictures or a tag to understand that this is exactly your picture, and it’s not clear how to find them. take an idiotic decision in order to take something like self.view.subviews.lastObject

    • Thanks a lot, helped! The first application just started writing, I do not know many of the subtleties! - snep
    • This is never a subtlety, it is a fairly simple logic - you have a component that is a container for displaying pictures, and it can display different pictures, why do you need three at once, if you want to display only one? - aknew
    • I agree, there is no need! Thanks for the clarification. - snep