码迷,mamicode.com
首页 > 移动开发 > 详细

iOS UI基础-3.0图片浏览器及plist使用

时间:2015-08-20 23:44:51      阅读:347      评论:0      收藏:0      [点我收藏+]

标签:

需求:
1.显示当前图片序号/总图片数
2.显示图片
3.上一张图片、下一张图片转换
4.显示图片描述
技术分享
下面用代码来实现
//
//  UYViewController.m
//  3.0图片查看器
//
//  Created by jiangys on 15/8/19.
//  Copyright (c) 2015年 uxiaoyuan. All rights reserved.
//

#import "UYViewController.h"

@interface UYViewController ()
@property(nonatomic,strong) UILabel *noLabel;
@property(nonatomic,strong) UIImageView *iconImage;
@property(nonatomic,strong) UILabel *descLable;
@property(nonatomic,strong) UIButton *leftButton;
@property(nonatomic,strong) UIButton *rightButton;

//显示当前的照片索引
@property(nonatomic,assign) int index;

//图片信息的数组
@property(nonatomic,strong) NSArray *imageList;

@end

@implementation UYViewController

-(NSArray *)imageList
{
    if (_imageList==nil) {
        NSString *path=[[NSBundle mainBundle] pathForResource:@"ImageList" ofType:@"plist"];
        
        //在OC中,通常需要完整的路径
        _imageList=[NSArray arrayWithContentsOfFile:path];
    }
    return _imageList;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    //1.索引
    _noLabel=[[UILabel alloc] initWithFrame:CGRectMake(0, 20, self.view.frame.size.width, 40)];
    _noLabel.text=@"1/5";
    _noLabel.textAlignment=NSTextAlignmentCenter;
    [self.view addSubview:_noLabel];
    
    //2.图片
    CGFloat imageW=200;
    CGFloat imageH=200;
    CGFloat imageX=(self.view.frame.size.width-imageW)*0.5;
    CGFloat imageY=CGRectGetMidY(self.noLabel.frame)+20;
    
    _iconImage=[[UIImageView alloc] initWithFrame:CGRectMake(imageX, imageY, imageW, imageH)];
    _iconImage.image=[UIImage imageNamed:@"biaoqingdi"];
    [self.view addSubview:_iconImage];
    
    //3.描述
    _descLable=[[UILabel alloc] initWithFrame:CGRectMake(0, CGRectGetMaxY(self.iconImage.frame), self.view.frame.size.width, 100)];
    _descLable.text=@"测试的拉";
    _descLable.textAlignment=NSTextAlignmentCenter;
    //需要Label具有“足够的高度”,不限制显示的行数
    _descLable.numberOfLines=0;
    [self.view addSubview:_descLable];
    
    //4.左边的按钮
    _leftButton=[[UIButton alloc] initWithFrame:CGRectMake(0, 0, 40, 40)];
    CGFloat centerX=self.iconImage.frame.origin.x*0.5;
    CGFloat centerY=self.iconImage.center.y;
    _leftButton.center=CGPointMake(centerX, centerY);
    _leftButton.tag=-1;
    
    [_leftButton setBackgroundImage:[UIImage imageNamed:@"left_normal"] forState:UIControlStateNormal];
    [_leftButton setBackgroundImage:[UIImage imageNamed:@"left_highlighted"] forState:UIControlStateHighlighted];
    [_leftButton addTarget:self action:@selector(clickButton:) forControlEvents:UIControlEventTouchUpInside];

    [self.view addSubview:_leftButton];
    
    //5.右边的按钮
    _rightButton=[[UIButton alloc] initWithFrame:CGRectMake(0, 0, 40, 40)];
    _rightButton.center=CGPointMake(self.view.bounds.size.width-centerX, centerY);
    
    [_rightButton setBackgroundImage:[UIImage imageNamed:@"right_normal"] forState:UIControlStateNormal];
    [_rightButton setBackgroundImage:[UIImage imageNamed:@"rigth_highlighted"] forState:UIControlStateHighlighted];
    [self.view addSubview:_rightButton];
    _rightButton.tag=1;
    [_rightButton addTarget:self action:@selector(clickButton:) forControlEvents:UIControlEventTouchUpInside];
    
    [self showPhotoInfo];
}

-(void)clickButton:(UIButton *)button
{
    //根据按钮调整当前显示图片的索引
    self.index+=(int)button.tag;
    [self showPhotoInfo];
}

-(void)showPhotoInfo
{
    self.noLabel.text=[NSString stringWithFormat:@"%d/%d",self.index+1,5];
    self.iconImage.image=[UIImage imageNamed:self.imageList[self.index][@"name"]];
    self.descLable.text=self.imageList[self.index][@"desc"];
    
    self.rightButton.enabled=(self.index!=4);
    self.leftButton.enabled=(self.index!=0);
}

@end

plist文件的使用:

1.创建一个plist

技术分享

2.填写为ImageList.plist

技术分享

3.填入数据

技术分享

4.完成

技术分享

 

iOS UI基础-3.0图片浏览器及plist使用

标签:

原文地址:http://www.cnblogs.com/jys509/p/4746475.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!