ios.照片显示
#import"ViewController.h"
@interfaceViewController()
//标题标签
@property(nonatomic,strong)UILabel*titlelabel;
//左边按钮
@property(nonatomic,strong)UIButton*leftBtn;
//右边按钮
@property(nonatomic,strong)UIButton*rightBtn;
//显示图片
@property(nonatomic,strong)UIImageView*myImageView;
@property(nonatomic,strong)NSArray*imageNames;
@end
@implementationViewController
- (void)viewDidLoad {
[superviewDidLoad];
self.imageNames=@[@"biaoqingdi",@"bingli",@"chiniupa",@"danteng",@"wangba"];
//创建并初始化标签文本
self.titlelabel=[[UILabelalloc]initWithFrame:CGRectMake(150,300,150,30)];
//设置标签文本
self.titlelabel.text=@"biaoqingdi";
//将标签添加到视图
[self.viewaddSubview:self.titlelabel];
//创建并初始化左边按钮
self.leftBtn=[[UIButtonalloc]
initWithFrame:CGRectMake(20,150,45,45)];
//关闭交互
self.leftBtn.userInteractionEnabled=NO;
//根据名字加载图片
UIImage*leftImage=[UIImageimageNamed:@"left_disable"];
//给左边按钮设置背景图片
[self.leftBtnsetBackgroundImage:leftImageforState:
(UIControlStateNormal)];
//设置左边按钮显示的图片
[self.viewaddSubview:self.leftBtn];
//创建并初始化相框
self.myImageView=[[UIImageViewalloc]initWithFrame:CGRectMake(85,100,200,200)];
//根据名字加载图片
UIImage*image=[UIImageimageNamed:@"biaoqingdi"];
self.myImageView.image=image;
//设置myImageView显示的图片
[self.viewaddSubview:self.myImageView];
//创建并初始化右边按钮
self.rightBtn=[[UIButtonalloc]initWithFrame:CGRectMake(305,150,45,45)];
//根据名字加载图片
UIImage*rightImage=[UIImageimageNamed:@"right_normal"];
//设置右边按钮的背景图片
[self.rightBtnsetBackgroundImage:rightImageforState:(UIControlStateNormal)];
//设置右边按钮显示的图片
[self.viewaddSubview:self.rightBtn];
[self.rightBtnaddTarget:selfaction:@selector(rightBtnAction)forControlEvents:(UIControlEventTouchUpInside)];
[self.leftBtnaddTarget:selfaction:@selector(leftBtnAction)forControlEvents:(UIControlEventTouchUpInside)];
}
-(void)rightBtnAction
{
//切换到下一张图片
//获取当前是第几张图片
NSIntegerindex=[self.imageNamesindexOfObject:self.titlelabel.text];
//不是为最后一张才切换到最后一张
if(index<4){
if(index==3){
//改变右边按钮颜色和关闭交互
self.rightBtn.userInteractionEnabled=NO;
UIImage*image=[UIImageimageNamed:@"right_disable"];
[self.rightBtnsetBackgroundImage:imageforState:(UIControlStateNormal)];
}else{
//左边按钮和右边按钮都是在一个正常状态
self.leftBtn.userInteractionEnabled=YES;
UIImage*leftNormal=[UIImageimageNamed:@"left_normal"];
UIImage*rightNormal=[UIImageimageNamed:@"right_normal"];
[self.leftBtnsetBackgroundImage:leftNormalforState:(UIControlStateNormal)];
[self.rightBtnsetBackgroundImage:rightNormalforState:(UIControlStateNormal)];
}
//通过下标访问下一张图片
NSString*nextTitle=self.imageNames[index+1];
self.titlelabel.text=nextTitle;
//设置相框显示的下一张图片
self.myImageView.image=[UIImageimageNamed:nextTitle];
}
}
-(void)leftBtnAction
{
NSIntegerindex=[self.imageNamesindexOfObject:self.titlelabel.text];
//不是第一张的时候才切换到上一张
if(index>0){
if(index==1){
//左边按钮交互关闭,图片切换
self.leftBtn.userInteractionEnabled=NO;
UIImage*image=[UIImageimageNamed:@"left_disable"];
[self.leftBtnsetBackgroundImage:imageforState:(+UIControlStateNormal)];
}else{
//左右两边按钮都是正常状态
self.leftBtn.userInteractionEnabled=YES;
UIImage*leftNormal=[UIImageimageNamed:@"left_normal"];
UIImage*rightNormal=[UIImageimageNamed:@"right_normal"];
[self.leftBtnsetBackgroundImage:leftNormalforState:(UIControlStateNormal)];
[self.rightBtnsetBackgroundImage:rightNormalforState:(UIControlStateNormal)];
self.rightBtn.userInteractionEnabled=YES;
}
NSString*preTitle=self.imageNames[index-1];
self.titlelabel.text=preTitle;
self.myImageView.image=[UIImageimageNamed:preTitle];
}
}
- (void)didReceiveMemoryWarning{
[superdidReceiveMemoryWarning];
// Dispose of any resources that can be
recreated.
}
@end
n�˜'�_�