Cat Share, must boutique
First look at the effect
The main implementation of similar to see the news of an interface, without dragging, pure code manual writing.
The first analysis of the app makes it easy to know that he has two uilabel, one Uiimageview and two UIButton.
Define what's in UIView.
@property(nonatomic,Strong)UILabel*nolabel;//Digital label@property(nonatomic,Strong)Uiimageview*iconimage;//Picture control@property(nonatomic,Strong)UILabel*desclabel;//Description information@property(nonatomic,Strong)UIButton*leftbutton;//Left button@property(nonatomic,Strong)UIButton*rightbutton;//Right button
The next step is to instantiate each control to do, and at the beginning I was writing directly in the-(void) Viewdidload method, and later because I learned lazy loading
The design pattern (which feels like a lazy Fellow in Java design mode) optimizes the code, which is directly optimized.
Lazy Loading
Lazy loading design is mainly to put UI controls into the defined control's Get method instantiation, so that can reduce the code in the Viewdidload in the upper and lower relations, can be effectively decoupled.
Uilabel:nolabel
-(uilabel *) noLabel{ if (_nolabel = = nil ) {//1, ordinal. _nolabel = [[uilabel alloc] Initwithframe:cgrectmake ( 0 , 40 , self .view .bounds .size .width , 40 )]; _nolabel.textalignment = Nstextalignmentcenter; [self .view Addsubview:_nolabel]; } return _nolabel;}
[Self.view Addsubview:_nolabel]; This is to hang the control on top of the view, the picture must be hung, or no one will see.
Note: * Focus, in the Get method can not write Self.nolabel; do not use the "dot" syntax, this will cause the Get method to die loop, because the "point" syntax is called the Get method, so to use the underscore attribute name method to get the object (in memory this is actually a pointer).
Uiimageview:iconimage
-(Uiimageview*) iconimage{if(_iconimage = =Nil){//2, Image CGFloatImagew = $;//width of the image control CGFloatImageh = $;//height of the image control CGFloatImageX = ( Self. View. Bounds. Size. Width-Imagew) *0.5;//X coordinate position of the image control CGFloatImagey = Cgrectgetmaxy ( Self. Nolabel. Frame) + -;//y-coordinate position of the image control_iconimage = [[UiimageviewAlloc] Initwithframe:cgrectmake (ImageX, Imagey, Imagew, Imageh)]; [ Self. ViewAddsubview:_iconimage]; }return_iconimage;}
Just like the last one, I added in the comments.
-(UILabel*) desclabel{if(_desclabel = =Nil){//3, Description CGFloatDescy = Cgrectgetmaxy ( Self. Iconimage. Frame); _desclabel = [[UILabelAlloc] Initwithframe:cgrectmake (0, Descy, Self. View. Bounds. Size. Width, -)];//Automatic line wrapping_desclabel. NumberOfLines=0;//Adjust Text center display_desclabel. TextAlignment= Nstextalignmentcenter; [ Self. ViewAddsubview:_desclabel]; }return_desclabel;}
This is a description, a more automatic line-wrapping method _desclabel.numberoflines = 0;
UIButton LeftButton
-(UIButton*) leftbutton{if(_leftbutton = =Nil) {//4. button on the left_leftbutton = [[UIButtonAlloc] Initwithframe:cgrectmake (0,0, +, +)];CGFloatCenty = Self. Iconimage. Center. Y;CGFloatCentx = Self. Iconimage. Frame. Origin. x*0.5; _leftbutton. Center= Cgpointmake (Centx, Centy);//Set default button display status[_leftbutton setbackgroundimage:[UIImageimagenamed:@"Left_normal"] Forstate:uicontrolstatenormal];//Set highlighting to appear when the button is pressed[_leftbutton setbackgroundimage:[UIImageimagenamed:@"left_highlighted"] forstate:uicontrolstatehighlighted]; _leftbutton. Tag= -1;//Set the tag of the button[ Self. ViewAddsubview:_leftbutton]; }return_leftbutton;} -(UIButton*) rightbutton{if(_rightbutton = =Nil) {//5. button on the right CGFloatCentx = Self. Iconimage. Frame. Origin. x*0.5;CGFloatCenty = Self. Iconimage. Center. Y; _rightbutton = [[UIButtonAlloc] Initwithframe:cgrectmake (0,0, +, +)]; _rightbutton. Center= Cgpointmake ( Self. View. Bounds. Size. Width-Centx,centy);//Set the button by default[_rightbutton setbackgroundimage:[UIImageimagenamed:@"Right_normal"] Forstate:uicontrolstatenormal];//Set highlighting[_rightbutton setbackgroundimage:[UIImageimagenamed:@"right_highlighted"] forstate:uicontrolstatehighlighted]; _rightbutton. Tag=1; [ Self. ViewAddsubview:_rightbutton]; }return_rightbutton;//Set the tag of the button}
Here set the left and right buttons, start those do not say, see cgfloat we should be able to instantly think of layout position what those keywords corresponding
CGRect Cgsize Cgpoint and three other frame bounds Center
Here is a very subtle design, that is, tag set the tag to 1 and 1 in the back will be magical.
Picture Collection
//当前显示的照片索引@property (nonatomicassignint index;//图片的集合@property (nonatomicstrongNSArray *imageList;
//Display picture information- (void) showphotoinfo{//Add content to the serial number from the ImageList array to get Self. Nolabel. Text= [NSStringstringwithformat:@"%d/%d", Self. Index+1, Self. ImageList. Count];//Add content to the picture from the ImageList array to get Self. Iconimage. Image= [UIImageImagenamed: Self. ImageList[ Self. Index][@"Name"]];//Add content to the description from the ImageList array to get Self. Desclabel. Text= Self. ImageList[ Self. Index][@"desc"]; Self. LeftButton. Enabled= ( Self. Index!=0);//When indexing to the first picture, let the left button program not press the state Self. Rightbutton. Enabled= ( Self. Index!= Self. ImageList. Count-1);//When indexing to the last picture, let the right side button program not to press the state}- (Nsarray*) imagelist{if(_imagelist = =Nil) {//Set the path of the content (plist) //Contentsoffile in OC, usually requires full path NSString*path = [[NSBundleMainbundle] pathforresource:@"ImageList"oftype:@"Plist"]; _imagelist = [NsarrayArraywithcontentsoffile:path];NSLog(@"%@", _imagelist); }return_imagelist;}
The _imagelist = [Nsarray Arraywithcontentsoffile:path] is used here;
To get the stuff we need from the Imagelist.plist file we've set up.
Click events and Call
- (void) viewdidload{[SuperViewdidload];//Display information[ SelfShowphotoinfo];//button Click event Call[_leftbutton AddTarget: SelfAction@selector(Chickbutton:) forcontrolevents:uicontroleventtouchupinside]; [_rightbutton AddTarget: SelfAction@selector(Chickbutton:) forcontrolevents:uicontroleventtouchupinside]; }//Click Button Events- (void) Chickbutton: (UIButton*) button{ Self. Index+ = button. Tag; [ SelfShowphotoinfo];}
Here we use a very subtle place, remember the previous Tag property.
Here we directly self.index + = Button.tag; The button is then implemented to optimize the code by pressing the left index to self-increment the right.
[_leftbutton addtarget:self Action: @selector (Chickbutton:) forcontrolevents:uicontroleventtouchupinside];
[_rightbutton addtarget:self Action: @selector (Chickbutton:) forcontrolevents:uicontroleventtouchupinside];
}
These two methods create a listener, so you can click on the calling code.
All right, the position here is complete. Don't use the front section to drag and drop it.
PS1. Need to learn the information can be added to my znycat (where to see, the learning materials are free, including video, PPT, projects, project materials ..... ) 。 ———— I just too much to learn, I want to find a few people with me tangled together.
Cat Learn iOS (iii) UI pure code implementation ui--Picture Viewer