Cats to share. Must boutique
Look at the effect
The main realization of a similar look at the news of an interface, without dragging, pure code hand-written.
First analysis of the app can be very easy to know that he has two Uilabel a 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;//Descriptive narrative 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 written directly in the-(void) Viewdidload method, and later because of learning lazy loading
The design pattern (feeling like a lazy Fellow in Java design mode) optimizes the code, which is directly optimized.
Lazy Loading
Lazy loading is designed to instantiate a UI control in the Get method of a defined control, which reduces the context of the code in Viewdidload. 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. Do not write Self.nolabel in the Get method, do not use "dot" syntax, this will cause the Get method to die loop. Because the "point" syntax is called the Get method, the object is obtained using the underscore property name (in memory, which 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;}
It's almost the same as the last one, and I've joined in the gaze.
-(UILabel*) desclabel{if(_desclabel = =Nil){//3. Description CGFloatDescy = Cgrectgetmaxy ( Self. Iconimage. Frame); _desclabel = [[UILabelAlloc] Initwithframe:cgrectmake (0, Descy, Self. View. Bounds. Size. Width, -)];//Take your own initiative to change lines_desclabel. NumberOfLines=0;//Adjust Text center display_desclabel. TextAlignment= Nstextalignmentcenter; [ Self. ViewAddsubview:_desclabel]; }return_desclabel;}
This is a description of the narrative. More than one of their own initiative 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 button display status by default[_leftbutton setbackgroundimage:[UIImageimagenamed:@"Left_normal"] Forstate:uicontrolstatenormal];//Set highlight to display when button is pressed[_leftbutton setbackgroundimage:[UIImageimagenamed:@"left_highlighted"] forstate:uicontrolstatehighlighted]; _leftbutton. Tag= -1;//Set button's tag[ 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 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 button's tag}
The left and right buttons are set here, and they don't start. When we see cgfloat, we should be able to think of the layout position, what those keywords correspond to.
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 Self. Nolabel. Text= [NSStringstringwithformat:@"%d/%d", Self. Index+1, Self. ImageList. Count];//Add the contents of the picture from the ImageList array to get Self. Iconimage. Image= [UIImageImagenamed: Self. ImageList[ Self. Index][@"Name"]];//Add content to descriptive narrative 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 The state that cannot be pressed}- (Nsarray*) imagelist{if(_imagelist = =Nil) {//Set the path of the content (plist) //Contentsoffile in OC, usually requires a complete 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 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 Event- (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 establish monitoring. Well, then you can click on the calling code.
All right, here we go. Don't use the front section to drag and drop it.
Cat learning iOS (c) UI plain code ui--picture Browser