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

时间:2023-03-09 05:38:38
iOS UI基础-3.0图片浏览器及plist使用
需求:
1.显示当前图片序号/总图片数
2.显示图片
3.上一张图片、下一张图片转换
4.显示图片描述
iOS UI基础-3.0图片浏览器及plist使用
下面用代码来实现
//
// 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(, , self.view.frame.size.width, )];
_noLabel.text=@"1/5";
_noLabel.textAlignment=NSTextAlignmentCenter;
[self.view addSubview:_noLabel]; //2.图片
CGFloat imageW=;
CGFloat imageH=;
CGFloat imageX=(self.view.frame.size.width-imageW)*0.5;
CGFloat imageY=CGRectGetMidY(self.noLabel.frame)+; _iconImage=[[UIImageView alloc] initWithFrame:CGRectMake(imageX, imageY, imageW, imageH)];
_iconImage.image=[UIImage imageNamed:@"biaoqingdi"];
[self.view addSubview:_iconImage]; //3.描述
_descLable=[[UILabel alloc] initWithFrame:CGRectMake(, CGRectGetMaxY(self.iconImage.frame), self.view.frame.size.width, )];
_descLable.text=@"测试的拉";
_descLable.textAlignment=NSTextAlignmentCenter;
//需要Label具有“足够的高度”,不限制显示的行数
_descLable.numberOfLines=;
[self.view addSubview:_descLable]; //4.左边的按钮
_leftButton=[[UIButton alloc] initWithFrame:CGRectMake(, , , )];
CGFloat centerX=self.iconImage.frame.origin.x*0.5;
CGFloat centerY=self.iconImage.center.y;
_leftButton.center=CGPointMake(centerX, centerY);
_leftButton.tag=-; [_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(, , , )];
_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=;
[_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+,];
self.iconImage.image=[UIImage imageNamed:self.imageList[self.index][@"name"]];
self.descLable.text=self.imageList[self.index][@"desc"]; self.rightButton.enabled=(self.index!=);
self.leftButton.enabled=(self.index!=);
} @end

plist文件的使用:

1.创建一个plist

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

2.填写为ImageList.plist

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

3.填入数据

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

4.完成

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