ios12--Simple Shopping Cart

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.