ios16--Custom Controls 1

Source: Internet
Author: User

K Controller:

// //   XMGViewController.h#import <UIKit/UIKit.h>@interface  Xmgviewcontroller : Uiviewcontroller@end
////XMGVIEWCONTROLLER.M#import "XMGViewController.h"#import "XMGShop.h"#import "XMGShopView.h"@interfaceXmgviewcontroller ()//Shopping Cart@property (Weak, nonatomic) Iboutlet UIView *Shopcarview;//Add button@property (Weak, nonatomic) Iboutlet UIButton *AddButton;//Delete button@property (Weak, nonatomic) Iboutlet UIButton *RemoveButton;/** Data Array*/@property (nonatomic, strong) Nsarray*Dataarr;@end@implementationXmgviewcontroller/** Lazy Loading*/-(Nsarray *) dataarr{if(_dataarr = =Nil) {        //Loading Data//1. Get the full pathNSString *datapath = [[NSBundle mainbundle] Pathforresource:@"shopdata.plist"Oftype:nil]; Self.dataarr=[Nsarray Arraywithcontentsoffile:datapath]; //Dictionary Turn Model//creating a temporary arrayNsmutablearray *temparray =[Nsmutablearray array];  for(Nsdictionary *dictinchSelf.dataarr) {//Create Shop ObjectsXmgshop *shop =[Xmgshop shopwithdict:dict]; //load the model into an array[Temparray Addobject:shop]; } Self.dataarr=Temparray; }    return_dataarr;}//Initializing Data- (void) viewdidload {[Super viewdidload];}/** * 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 *****************************/    /*Original Separate creation://1. Create a view UIView *shopview = [[UIView alloc] init];      2. Set Frame Shopview.frame = CGRectMake (x, y, width, height);      3. Set the background color Shopview.backgroundcolor = [Uicolor Greencolor];      4. Add to Cart [Self.shopcarview Addsubview:shopview];    5. Create the Uiimageview object of the product uiimageview *iconview = [[Uiimageview alloc] init];    Iconview.frame = CGRectMake (0, 0, width, width);    Iconview.backgroundcolor = [Uicolor Bluecolor];      [Shopview Addsubview:iconview];    6. Create a product title object UILabel *titlelabel = [[UILabel alloc] init];    Titlelabel.frame = CGRectMake (0, Width, width, height-width);    Titlelabel.backgroundcolor = [Uicolor Yellowcolor]; Titlelabel.textalignment = Nstextalignmentcenter;     Center [Shopview Addsubview:titlelabel]; */Xmgshopview*shopview =[[Xmgshopview alloc] init]; Shopview.frame=CGRectMake (x, y, width, height);    [Self.shopcarview Addsubview:shopview]; /***********************3. Setting up data *****************************/    //Setting up dataXmgshop *shop =Self.dataarr[index]; ShopView.iconView.image=[UIImage ImageNamed:shop.icon]; ShopView.titleLabel.text=Shop.name; /***********************4. Setting the state of the button *****************************/button.enabled= (Index! =5); //5. Set the status of the delete buttonself.removeButton.enabled =YES; }/** * Remove from cart * * @param button*/-(Ibaction) Remove: (UIButton *) button {//1. Delete the last itemUIView *lastshopview =[Self.shopCarView.subviews Lastobject];        [Lastshopview Removefromsuperview]; //3. Set the status of the Add buttonself.addButton.enabled =YES; //4. Set the status of the delete buttonself.removeButton.enabled = (Self.shopCarView.subviews.count! =0); }@end

custom controls;

// //   XMGShopView.h#import <UIKit/UIKit.h>@interface  xmgshopview:uiview /*  */*iconview; // [self addsubview:iconview]; there is already a strong pointer reference, here with weak /* * * *Titlelabel;    @end
////xmgshopview.m#import "XMGShopView.h"@interfaceXmgshopview ()@end@implementationXmgshopview/** * Initialize child controls (do not set frame, get width height is 0, can be obtained at layoutsubviews) **/-(instancetype) init{if(self =[Super Init]) {        /*//0. Gets the size of the current control cgfloat width = self.frame.size.width; CGFloat height = self.frame.size.height;        NSLog (@ "init:%f----%f", width, height); */        //1. Create a Uiimageview object for a productUiimageview *iconview =[[Uiimageview alloc] init];//iconview.frame = CGRectMake (0, 0, width, width);Iconview.backgroundcolor =[Uicolor Bluecolor];        [Self addsubview:iconview]; Self.iconview=IconView; //2. Create a product Title objectUILabel *titlelabel =[[UILabel alloc] init];//titlelabel.frame = CGRectMake (0, Width, width, height-width);Titlelabel.backgroundcolor =[Uicolor Yellowcolor]; Titlelabel.textalignment= Nstextalignmentcenter;//Center[Self Addsubview:titlelabel];//The strong pointer references theSelf.titlelabel = Titlelabel;//Titlelabel becomes global, otherwise the Layoutsubviews () method cannot get inside,    }    returnSelf ;}/** * Layout Sub-controls (can get frame)*/- (void) layoutsubviews{//0. Be sure to call super to preserve the layout of the parent class system. Otherwise, the layout of the parent class is gone. [Super Layoutsubviews]; //1. Get the dimensions of the current controlCGFloat width = self.frame.size.width;//This frame is from the outside Shopview.frame = CGRectMake (x, y, width, height);CGFloat height =Self.frame.size.height; //2. Set the frame of the child controlSelf.iconView.frame = CGRectMake (0,0, width, width); Self.titleLabel.frame= CGRectMake (0, width, width, height-width);}@end

Bean:

////XMGShop.h#import<Foundation/Foundation.h>@interfaceXmgshop:nsobject/** Name of the picture*/@property (nonatomic, copy) NSString*icon;/** Name of the product*/@property (nonatomic, copy) NSString*name;//provides construction methods/*-(Instancetype) Initwithicon: (NSString *) icon Name: (NSString *) name;+ (instancetype) Shopwithicon: (NSString *) Icon Name: (NSString *) name; */-(Instancetype) Initwithdict: (Nsdictionary *) dict;+ (Instancetype) shopwithdict: (Nsdictionary *) dict;@end
////xmgshop.m#import "XMGShop.h"@implementationXmgshop/*-(Instancetype) Initwithicon: (NSString *) icon Name: (NSString *) name{if (self = [super init]) {Self.icon =        Icon    Self.name = name; } return self;} + (Instancetype) Shopwithicon: (NSString *) icon Name: (NSString *) name{return [[Self alloc] Initwithicon:icon name:name]; } */-(Instancetype) Initwithdict: (Nsdictionary *) dict{if(self =[Super Init]) {Self.icon= dict[@"icon"]; Self.name= dict[@"name"]; }    returnSelf ;}+ (Instancetype) shopwithdict: (Nsdictionary *) dict{return[Self alloc] initwithdict:dict];}@end

ios16--Custom Controls 1

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.