iOS.Lover
iOS.Lover

Reputation: 6051

Remove and add multiple image in same UIImageView

I am designing a make own pizza app. The user would add multiple toppings for example shrimp, bacon, and etc. on top of each other. So the adding image is working fine. Here is my code:

bool meatAdded;
- (void)addMeatTopping:(NSString*)meat  withImage:(UIImageView*)meatImage {
    if ((meatAdded =! meatAdded)) {
        //crust o taqir bede be cheese BADAN
        meatImage = [[UIImageView alloc]initWithFrame:_crustImage.bounds];
        [meatImage setImage:[UIImage imageNamed:meat]];
        [_crustImage addSubview:meatImage];

        for (UIButton*button in _meatButtonsArray) {
            checkMark.center = button.center;
            checkMark.alpha = 1;
        }

        [UIView animateWithDuration:.60 delay:0 usingSpringWithDamping:.40 initialSpringVelocity:.20 options:UIViewAnimationOptionAllowUserInteraction animations:^ {
            meatImage.transform = CGAffineTransformMakeScale(.5, .5);
            meatImage.transform = CGAffineTransformMakeScale(1, 1);             } completion:nil];
    }else {
        /**** THIS PART OF CODE DOESN'T WORK ! IT PRINTS LOG BUT DOESN'T CHANGE THE MEAT IMAGE AT ALL ****/

        NSLog(@"Remove");
        meatImage.alpha = 0;
        [meatImage setImage:nil];
        [meatImage removeFromSuperview];
    }
}

example of calling above method:

- (IBAction)meat0:(id)sender {
    [self addMeatTopping:@"topping_Bacon" withImage:beykenMeat];
}

- (IBAction)meat1:(id)sender {
   [self addMeatTopping:@"Steak" withImage:steykMeat];
}

How can I create a toggle method when user select an item? The item will add to view and when taps again it should be removed from view.

Upvotes: 0

Views: 156

Answers (1)

Himanshu Sraswat
Himanshu Sraswat

Reputation: 534

SubclassImageView *imageviewG = [[SubclassImageView alloc]initWithFrame:CGRectMake(0, 0, 200, 200)];
self.crustImage = [[UIView alloc]initWithFrame:CGRectMake(50, 50, 250, 250)];
self.crustImage.backgroundColor = [UIColor redColor];
[imageviewG setImage:@"topping_Bacon"];

[self.crustImage addSubview:imageviewG];
[self.view addSubview:self.crustImage];

    for(id viewInner in self.crustImage.subviews){
    if([viewInner isKindOfClass:[SubclassImageView class]]){
        SubclassImageView *imageSeleted = (SubclassImageView *)viewInner;
        if([imageSeleted.getImageName isEqualToString:@"topping_Bacon"]){
        //your logic to add or remove the imageview from super view
        }else if([imageSeleted.getImageName isEqualToString:@"Steak"]){
        //your logic to add or remove the imageview from super view
        }
    }
}

Upvotes: 1

Related Questions