iOS Tutorials (3)--uiimageview, UILabel, UIButton implement a small case

Source: Internet
Author: User

Wait a minute, we're going to do this. See the code below (the code has not been optimized to look at the basic) ( da da: Just seemed to feel very difficult );

viewcontroller.m//03 Picture Browser (code creation)////Created by Sunda on 15/7/1.//Copyright (c) 2015 Sunda. All rights reserved.//#import "ViewController.h" @interface viewcontroller ()/** * Serial number */@property (strong,nonatomic) UILabel *orderlable;/** * Picture */@property (Nonatomic,strong) uiimageview *iconimageview;/** * Picture Description */@property (nonatomic, Strong) UILabel *detailslable;/** * LEFT arrow */@property (Nonatomic,strong) UIButton *leftbutton;/** * RIGHT ARROW */@property (Nona Tomic,strong) UIButton *rightbutton;/** * Count */@property (nonatomic,assign) int index; @end @implementation    viewcontroller-(void) viewdidload {[Super viewdidload];    Build serial number self.orderlable = [[UILabel alloc] init];    Self.orderLable.frame = CGRectMake (0, self.view.frame.size.width,40);    Self.orderLable.text = @ "1/5";    Set text centered self.orderLable.textAlignment = Nstextalignmentcenter;    [Self.view addSubview:self.orderLable];    Build Picture Self.iconimageview = [[Uiimageview alloc] init];    float iconw = 200; FloatIconh = 200;    float icony = Cgrectgetmaxy (self.orderLable.frame) + 20;    float IconX = (self.view.frame.size.width-iconw)/2;    Self.iconImageView.frame = CGRectMake (IconX, Icony, Iconw, Iconh);    Self.iconImageView.image = [UIImage imagenamed:@ "Biaoqingdi"];        [Self.view AddSubview:self.iconImageView];    Build picture Description self.detailslable = [[UILabel alloc] init];    CGFloat detailsy = Cgrectgetmaxy (self.iconImageView.frame) + 20;    Self.detailsLable.frame = CGRectMake (0, Detailsy, self.view.frame.size.width,100);    Self.detailsLable.text = @ "God horse expression";    Self.detailsLable.backgroundColor = [Uicolor Redcolor];    Set text centered self.detailsLable.textAlignment = Nstextalignmentcenter;        [Self.view addSubview:self.detailsLable];    Create left arrow Self.leftbutton = [[UIButton alloc] init];    Self.leftButton.frame = CGRectMake (0, 0, 40, 40);    [Self.leftbutton setbackgroundimage:[uiimage imagenamed:@ "Left_normal"] forstate:uicontrolstatenormal]; [Self.leftbutton SetbackgroundimAge:[uiimage imagenamed:@ "left_highlighted"] forstate:uicontrolstatehighlighted];    Self.leftButton.center = Cgpointmake (SELF.ICONIMAGEVIEW.FRAME.ORIGIN.X/2, SELF.ICONIMAGEVIEW.CENTER.Y);    [Self.view AddSubview:self.leftButton];        [Self.leftbutton addtarget:self Action: @selector (Leftclick) forcontrolevents:uicontroleventtouchupinside];    Create right arrow Self.rightbutton = [[UIButton alloc] init];    Self.rightButton.frame = CGRectMake (0, 0, 40, 40);    [Self.rightbutton setbackgroundimage:[uiimage imagenamed:@ "Right_normal"] forstate:uicontrolstatenormal]; [Self.rightbutton setbackgroundimage:[uiimage imagenamed:@ "right_highlighted"] forstate:uicontrolstatehighlighted    ]; Self.rightButton.center = Cgpointmake (self.view.frame.size.width-self.leftbutton.center.x,    SELF.ICONIMAGEVIEW.CENTER.Y);    [Self.view AddSubview:self.rightButton];        [Self.rightbutton addtarget:self Action: @selector (RightClick) forcontrolevents:uicontroleventtouchupinside]; [Self changeimage];}/** Modifying image data */-(void) changeimage{//Setting the display of all controls above the UI//display ordinal label according to Self.index, image, image description Self.orderLable.text = [Nsstr    ing stringwithformat:@ "%d/%d", Self.index + 1, 5];            Switch (self.index) {Case 0:self.iconimageview.image = [UIImage imagenamed:@ "Biaoqingdi"];            Self.detailsLable.text = @ "expression";        Break            Case 1:self.iconimageview.image = [UIImage imagenamed:@ "Bingli"];            Self.detailsLable.text = @ "Case";        Break            Case 2:self.iconimageview.image = [UIImage imagenamed:@ "Chiniupa"];            Self.detailsLable.text = @ "Eat steak";        Break            Case 3:self.iconimageview.image = [UIImage imagenamed:@ "Danteng"];            Self.detailsLable.text = @ "Egg hurts";        Break            Case 4:self.iconimageview.image = [UIImage imagenamed:@ "Wangba"];            Self.detailsLable.text = @ "Wang Ba";    Break    } self.leftButton.enabled = (Self.index! = 0); Self.rightButton.enabled = (Self.index! = 4);    }/** Left button click Method */-(void) leftclick{NSLog (@ "left");    Self.index--;    [Self changeimage];     }/** Right button click Method */-(void) rightclick{NSLog (@ "right");    Self.index + +;    [Self changeimage]; } @end
is not very simple (da da: Very difficult, decisive to hit the code .... )

Code Optimization one switch optimization

Important I added a comment in the code

viewcontroller.m//03 Picture Browser (code creation)////Created by Sunda on 15/7/1.//Copyright (c) 2015 Sunda. All rights reserved.//#import "ViewController.h" @interface viewcontroller ()/** * Serial number */@property (strong,nonatomic) UILabel *orderlable;/** * Picture */@property (Nonatomic,strong) uiimageview *iconimageview;/** * Picture Description */@property (nonatomic, Strong) UILabel *detailslable;/** * LEFT arrow */@property (Nonatomic,strong) UIButton *leftbutton;/** * RIGHT ARROW */@property (Nona Tomic,strong) UIButton *rightbutton;/** * Count */@property (nonatomic,assign) int index;/** * image list */@property (nonatomic, S Trong) Nsarray *imagelist; @end @implementation viewcontroller//lazy Loading-when needed, in instantiation loaded into memory-(Nsarray *) imagelist{//Only First Call When the getter method is empty, it instantiates and establishes an array if (_imagelist = = nil) {//file represents the full path from the files loaded with the file//bundle-package, read-only nsstring *p        Ath = [[NSBundle mainbundle] pathforresource:@ "ImageData" oftype:@ "plist"];    _imagelist = [Nsarray Arraywithcontentsoffile:path]; } return _imagelist;} -(void)viewdidload {[Super viewdidload];    Build serial number self.orderlable = [[UILabel alloc] init];    Self.orderLable.frame = CGRectMake (0, self.view.frame.size.width,40);    Self.orderLable.text = @ "1/5";    Set text centered self.orderLable.textAlignment = Nstextalignmentcenter;    [Self.view addSubview:self.orderLable];    Build Picture Self.iconimageview = [[Uiimageview alloc] init];    float iconw = 200;    float Iconh = 200;    float icony = Cgrectgetmaxy (self.orderLable.frame) + 20;    float IconX = (self.view.frame.size.width-iconw)/2;    Self.iconImageView.frame = CGRectMake (IconX, Icony, Iconw, Iconh);    Self.iconImageView.image = [UIImage imagenamed:@ "Biaoqingdi"];        [Self.view AddSubview:self.iconImageView];    Build picture Description self.detailslable = [[UILabel alloc] init];    CGFloat detailsy = Cgrectgetmaxy (self.iconImageView.frame) + 20;    Self.detailsLable.frame = CGRectMake (0, Detailsy, self.view.frame.size.width,100);    Self.detailsLable.text = @ "God horse expression"; self.detailslable.bAckgroundcolor = [Uicolor Redcolor];    Set text centered self.detailsLable.textAlignment = Nstextalignmentcenter;        [Self.view addSubview:self.detailsLable];    Create left arrow Self.leftbutton = [[UIButton alloc] init];    Self.leftButton.frame = CGRectMake (0, 0, 40, 40);    [Self.leftbutton setbackgroundimage:[uiimage imagenamed:@ "Left_normal"] forstate:uicontrolstatenormal];    [Self.leftbutton setbackgroundimage:[uiimage imagenamed:@ "left_highlighted"] forstate:uicontrolstatehighlighted];    Self.leftButton.center = Cgpointmake (SELF.ICONIMAGEVIEW.FRAME.ORIGIN.X/2, SELF.ICONIMAGEVIEW.CENTER.Y);    [Self.view AddSubview:self.leftButton];        [Self.leftbutton addtarget:self Action: @selector (Leftclick) forcontrolevents:uicontroleventtouchupinside];    Create right arrow Self.rightbutton = [[UIButton alloc] init];    Self.rightButton.frame = CGRectMake (0, 0, 40, 40);    [Self.rightbutton setbackgroundimage:[uiimage imagenamed:@ "Right_normal"] forstate:uicontrolstatenormal]; [Self.rightbutton setbackgroundimage:[uiimage imagenamed:@ "right_highlighted"] forstate:uicontrolstatehighlighted]; Self.rightButton.center = Cgpointmake (self.view.frame.size.width-self.leftbutton.center.x,    SELF.ICONIMAGEVIEW.CENTER.Y);    [Self.view AddSubview:self.rightButton];        [Self.rightbutton addtarget:self Action: @selector (RightClick) forcontrolevents:uicontroleventtouchupinside]; [Self changeimage];} /** Modifying image data */-(void) changeimage{//Setting the display of all controls above the UI//display ordinal label according to Self.index, image, image description Self.orderLable.text = [Nsstri    ng stringwithformat:@ "%d/%d", Self.index + 1, 5];    Self.iconImageView.image = [UIImage imagenamed:self.imagelist[self.index][@ "name"];    Self.detailsLable.text = self.imagelist[self.index][@ "desc"];    self.leftButton.enabled = (Self.index! = 0);   self.rightButton.enabled = (Self.index! = 4);    }/** Left button click Method */-(void) leftclick{NSLog (@ "left");    Self.index--;    [Self changeimage];     }/** Right button click Method */-(void) rightclick{NSLog (@ "right"); Self.index + +;    [Self changeimage]; } @end

Code optimization Two-control lazy loading

viewcontroller.m//03 Picture Browser (code creation)////Created by Sunda on 15/7/1.//Copyright (c) 2015 Sunda. All rights reserved.//#import "ViewController.h" @interface viewcontroller ()/** * Serial number */@property (strong,nonatomic) UILabel *orderlable;/** * Picture */@property (Nonatomic,strong) uiimageview *iconimageview;/** * Picture Description */@property (nonatomic, Strong) UILabel *detailslable;/** * LEFT arrow */@property (Nonatomic,strong) UIButton *leftbutton;/** * RIGHT ARROW */@property (Nona Tomic,strong) UIButton *rightbutton;/** * Count */@property (nonatomic,assign) int index;/** * image list */@property (nonatomic, S Trong) Nsarray *imagelist; @end @implementation viewcontroller//lazy Loading-when needed, in instantiation loaded into memory-(Nsarray *) imagelist{//Only First Call When the getter method is empty, it instantiates and establishes an array if (_imagelist = = nil) {//file represents the full path from the files loaded with the file//bundle-package, read-only nsstring *p        Ath = [[NSBundle mainbundle] pathforresource:@ "ImageData" oftype:@ "plist"];    _imagelist = [Nsarray Arraywithcontentsoffile:path]; } return _imagelist;} -(UilabEl *) orderlable{if (_orderlable = = nil) {_orderlable = [[UILabel alloc] init];        _orderlable.frame = CGRectMake (0, self.view.frame.size.width,40);        Set text centered _orderlable.textalignment = Nstextalignmentcenter;    [Self.view addsubview:_orderlable];       } return _orderlable;        }-(Uiimageview *) iconimageview{if (_iconimageview = = nil) {_iconimageview = [[Uiimageview alloc] init];        float iconw = 200;        float Iconh = 200;        float icony = Cgrectgetmaxy (self.orderLable.frame) + 20;        float IconX = (self.view.frame.size.width-iconw)/2;        _iconimageview.frame = CGRectMake (IconX, Icony, Iconw, Iconh);    [Self.view Addsubview:_iconimageview];   } return _iconimageview;        }-(UILabel *) detailslable{if (_detailslable = = nil) {_detailslable = [[UILabel alloc] init];       CGFloat detailsy = Cgrectgetmaxy (self.iconImageView.frame) + 20; _detailslable.frame = CGRectMake (0, Detailsy, seLF.VIEW.FRAME.SIZE.WIDTH,100);        Set text multiline display _detailslable.numberoflines = 0;        _detailslable.backgroundcolor = [Uicolor Redcolor];        Set text centered _detailslable.textalignment = Nstextalignmentcenter;    [Self.view addsubview:_detailslable];  } return _detailslable;        }-(UIButton *) leftbutton{if (_leftbutton = = nil) {_leftbutton = [[UIButton alloc] init];        _leftbutton.frame = CGRectMake (0, 0, 40, 40);        [_leftbutton setbackgroundimage:[uiimage imagenamed:@ "Left_normal"] forstate:uicontrolstatenormal];        [_leftbutton setbackgroundimage:[uiimage imagenamed:@ "left_highlighted"] forstate:uicontrolstatehighlighted];        _leftbutton.center = Cgpointmake (SELF.ICONIMAGEVIEW.FRAME.ORIGIN.X/2, SELF.ICONIMAGEVIEW.CENTER.Y);        [Self.view Addsubview:_leftbutton];    [_leftbutton addtarget:self Action: @selector (Leftclick) forcontrolevents:uicontroleventtouchupinside]; } return _leftbutton;} -(UIButton *) rightbutton{IF (_rightbutton = = nil) {_rightbutton = [[UIButton alloc] init];        _rightbutton.frame = CGRectMake (0, 0, 40, 40);        [_rightbutton setbackgroundimage:[uiimage imagenamed:@ "Right_normal"] forstate:uicontrolstatenormal];        [_rightbutton setbackgroundimage:[uiimage imagenamed:@ "right_highlighted"] forstate:uicontrolstatehighlighted]; _rightbutton.center = Cgpointmake (self.view.frame.size.width-self.leftbutton.center.x,        SELF.ICONIMAGEVIEW.CENTER.Y);        [Self.view Addsubview:_rightbutton];    [_rightbutton addtarget:self Action: @selector (RightClick) forcontrolevents:uicontroleventtouchupinside]; } return _rightbutton;}    -(void) viewdidload {[Super viewdidload]; [Self changeimage];} /** Modifying image data */-(void) changeimage{//Setting the display of all controls above the UI//display ordinal label according to Self.index, image, image description Self.orderLable.text = [Nsstri    ng stringwithformat:@ "%d/%d", Self.index + 1, 5];    Self.iconImageView.image = [UIImage imagenamed:self.imagelist[self.index][@ "name"]; SeLf.detailsLable.text = self.imagelist[self.index][@ "desc"];    self.leftButton.enabled = (Self.index! = 0);   self.rightButton.enabled = (Self.index! = 4);    }/** Left button click Method */-(void) leftclick{NSLog (@ "left");    Self.index--;    [Self changeimage];     }/** Right button click Method */-(void) rightclick{NSLog (@ "right");    Self.index + +;    [Self changeimage]; } @end

Code https://github.com/sunda1314520/03-(da da anyway I don't download)

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

iOS Tutorials (3)--uiimageview, UILabel, UIButton implement a small case

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.