貓學習IOS(三)UI純程式碼UI——圖片瀏覽器

來源:互聯網
上載者:User

標籤:

貓分享。必須精品

看看效果

主要實現相似看新聞的一個介面,不用拖拽,純程式碼手工寫。


首先分析app能夠非常easy知道他這裡有兩個UILabel一個UIImageView還有兩個UIButton

定義UIView中的東西
@property (nonatomic, strong) UILabel *noLabel;//數字標籤@property (nonatomic, strong) UIImageView *iconImage;//圖片控制項@property (nonatomic, strong) UILabel *descLabel;//描寫敘述資訊@property (nonatomic, strong) UIButton *leftButton;//左邊button@property (nonatomic, strong) UIButton *rightButton;//右邊button

接下來就是執行個體化每個控制項要做的了,開始的時候我是直接在- (void)viewDidLoad方法中寫的,後來由於學習了懶載入
設計模式(感覺跟java設計模式中的懶漢差點兒相同)最佳化了代碼,這裡就直接給出最佳化後的了。

懶載入

懶載入設計主要就是把UI控制項放到定義好的控制項的get方法中執行個體化,這樣呢能夠降低代碼在viewDidLoad中的上下關係。能夠有效解耦。

UILabel: noLabel
-(UILabel *)noLabel{    if (_noLabel == nil) {        //1,序號.        _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];這個是將控制項掛到view上面,畫好了一定要掛上,要不沒人看到。

注意:*重點。在get方法裡面不能寫self.noLabel;千萬不要用“點”文法,這樣會造成get方法死迴圈。由於“點”文法就是調用的get方法,所以要用底線屬性名稱的方法得到對象(在記憶體這事實上是一個指標)。

UIImageView: iconImage
-(UIImageView *)iconImage{    if(_iconImage == nil){        //2,映像        CGFloat imageW = 200;//影像控制的寬         CGFloat imageH = 200;//影像控制的高        CGFloat imageX = (self.view.bounds.size.width - imageW)*0.5;//影像控制的x座標位置         CGFloat imageY = CGRectGetMaxY(self.noLabel.frame) + 20;//影像控制的y座標位置         _iconImage = [[UIImageView alloc] initWithFrame:CGRectMake(imageX, imageY, imageW, imageH)];        [self.view addSubview:_iconImage];    }    return _iconImage;}

跟上一個差點兒相同,我在凝視裡面都加入了

-(UILabel *)descLabel{    if(_descLabel == nil){        //3。說明        CGFloat descY = CGRectGetMaxY(self.iconImage.frame);        _descLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, descY, self.view.bounds.size.width, 100)];        //自己主動換行        _descLabel.numberOfLines = 0;        //調整文本置中顯示        _descLabel.textAlignment = NSTextAlignmentCenter;        [self.view addSubview:_descLabel];    }    return _descLabel;}

這個是描寫敘述的。多了一個自己主動換行方法 _descLabel.numberOfLines = 0;

UIButton leftButton
-(UIButton *)leftButton{    if (_leftButton == nil) {        //4。

左邊的button _leftButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 40, 40)]; CGFloat centY = self.iconImage.center.y; CGFloat centX = self.iconImage.frame.origin.x * 0.5; _leftButton.center = CGPointMake(centX, centY); //設定預設情況button顯示狀況 [_leftButton setBackgroundImage:[UIImage imageNamed:@"left_normal" ] forState:UIControlStateNormal]; //設定高亮 當按下button時候顯示的樣子 [_leftButton setBackgroundImage:[UIImage imageNamed:@"left_highlighted" ] forState:UIControlStateHighlighted]; _leftButton.tag = -1;//設定button的tag [self.view addSubview:_leftButton]; } return _leftButton;}-(UIButton *)rightButton{ if (_rightButton == nil) { //5。右邊的button CGFloat centX = self.iconImage.frame.origin.x * 0.5; CGFloat centY = self.iconImage.center.y; _rightButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 40, 40)]; _rightButton.center = CGPointMake(self.view.bounds.size.width - centX,centY); //設定預設情況下button [_rightButton setBackgroundImage:[UIImage imageNamed:@"right_normal" ] forState:UIControlStateNormal]; //設定高亮 [_rightButton setBackgroundImage:[UIImage imageNamed:@"right_highlighted" ] forState:UIControlStateHighlighted]; _rightButton.tag = 1; [self.view addSubview:_rightButton]; } return _rightButton;//設定button的tag}

這裡設定了左右button,開始那些都不說了。看到CGFloat我們就應該能瞬間想到布局位置什麼那些關鍵字對應的
CGRect CGSize CGPoint 以及另外三個frame bounds center

這裡有個非常精妙的設計,那就是tag 把tag設定成了1和-1在後面會有妙用。

圖片集合
//當前顯示的照片索引@property (nonatomic, assign) int index;//圖片的集合@property (nonatomic, strong) NSArray *imageList;
//顯示圖片資訊- (void) showPhotoInfo{    //給序號加入內容 從imageList數組中拿到    self.noLabel.text = [NSString stringWithFormat:@"%d/%d",self.index+1,self.imageList.count];//    給圖片加入內容從imageList數組中拿到    self.iconImage.image = [UIImage imageNamed:self.imageList[self.index][@"name"]];    //    給描寫敘述加入內容從imageList數組中拿到    self.descLabel.text = self.imageList[self.index][@"desc"];    self.leftButton.enabled = (self.index != 0);//當索引到第一張圖片的時候。讓左邊button編程不能按的狀態    self.rightButton.enabled = (self.index != self.imageList.count-1);//當索引到最後圖片的時候。讓右邊邊button編程不能按的狀態}- (NSArray *)imageList{    if (_imageList == nil) {        //設定存放內容(plist)的路徑        //在oc中contentsOfFile,通常須要完整的路徑        NSString *path = [[NSBundle mainBundle] pathForResource:@"imageList" ofType:@"plist"];        _imageList = [NSArray arrayWithContentsOfFile:path];        NSLog(@"%@",_imageList);    }    return _imageList;}

這裡用到了_imageList = [NSArray arrayWithContentsOfFile:path];
來從我們設定好的imageList.plist檔案裡得到要用的東東

點擊事件以及調用
- (void)viewDidLoad{    [super viewDidLoad];    //顯示資訊    [self showPhotoInfo];    //button點擊事件調用    [_leftButton addTarget:self action:@selector(chickButton:) forControlEvents:UIControlEventTouchUpInside];    [_rightButton addTarget:self action:@selector(chickButton:) forControlEvents:UIControlEventTouchUpInside];    }//點擊button事件- (void) chickButton:(UIButton *)button{    self.index += button.tag;    [self showPhotoInfo];}

這裡我們用到了一個非常精妙的地方,還記得前面的tag屬性吧
這裡我們直接 self.index += button.tag; 然後實現了button按左邊index自增右邊自減從而最佳化代碼。
[_leftButton addTarget:self action:@selector(chickButton:) forControlEvents:UIControlEventTouchUpInside];
[_rightButton addTarget:self action:@selector(chickButton:) forControlEvents:UIControlEventTouchUpInside];
}
這兩個方法建立監聽。恩 這樣就能點擊調用代碼了。

好了 到這裡位置就算完畢了。沒用用前段拖拽吧。

貓學習IOS(三)UI純程式碼UI——圖片瀏覽器

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.