Assets.xcassets picture is dragged to the right inside.
////VIEWCONTROLLER.M//03-Comprehensive exercises//#import "ViewController.h"@interfaceViewcontroller ()//Shopping Cart@property (Weak, nonatomic) Iboutlet UIView *Shopcarview;//Add button@property (Weak, nonatomic) Iboutlet UIButton *AddButton;//Delete button@property (Weak, nonatomic) Iboutlet UIButton *RemoveButton;//The global subscript//@property (nonatomic, assign) Nsinteger index;@end@implementationViewcontroller- (void) viewdidload {[Super viewdidload]; //assign a value to the subscript//self.index = 0; // //Trimming superfluous parts (undesirable)//self.shopCarView.clipsToBounds = YES;}/** * Add to Cart * * @param button*/-(Ibaction) Add: (UIButton *) button {/***********************1. Define some constants *****************************/ //1. Total number of columnsNsinteger Allcols =3; //2. Width and height of the productCGFloat width = the; CGFloat height= -; //3. Finding the horizontal and vertical spacingCGFloat Hmargin = (self.shopcarview.frame.size.width-allcols * width)/(Allcols-1); CGFloat Vmargin= (Self.shopCarView.frame.size.height-2* height)/1; //4. Setting the indexNsinteger index =Self.shopCarView.subviews.count; //5. Find the X valueCGFloat x = (hmargin + width) * (index%allcols); CGFloat y= (vmargin + height) * (Index/allcols); /***********************2. Create a product *****************************/ //1. Create a view of a productUIView *shopview =[[UIView alloc] init]; //2. Set the frameShopview.frame =CGRectMake (x, y, width, height); //3. Setting the background colorShopview.backgroundcolor =[Uicolor Greencolor]; //4. Add to Cart[Self.shopcarview Addsubview:shopview]; /***********************3. Setting the state of the button *****************************///if (index = = 5) {//button.enabled = NO;// }button.enabled = (Index! =5); //5. Set the status of the delete buttonself.removeButton.enabled =YES; //let subscript +1//Self.index + = 1;}/** * Remove from cart * * @param button*/-(Ibaction) Remove: (UIButton *) button {//1. Delete the last itemUIView *lastshopview =[Self.shopCarView.subviews Lastobject]; [Lastshopview Removefromsuperview]; //2. Set the index value -1//Self.index-= 1; //3. Set the status of the Add buttonself.addButton.enabled =YES; //4. Set the status of the delete button /*if (Self.shopCarView.subviews.count = = 0) {self.removeButton.enabled = NO; } */self.removeButton.enabled= (Self.shopCarView.subviews.count! =0); }@end
ios12--Simple Shopping Cart