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.
ifstatement?if ((meatAdded =! meatAdded)). I doubt that is what you actually want since that negatesmeatAddedand then assigns the new value tomeatAddedand then finally checks to see if the new value is true or false.NSLog(@"Remove:%@", meatImage)-- didmeatImagesomehow becomenilsomewhere else?