#import“ViewController,h”
@interface Viewcontroller()
@propetry(natomoc,strong)UILabel *titlelabel; //标题标签
@propetry(natomoc,strong)UIButton *leftbtn; //左边按钮
@propetry(natomoc,strong)UIButton *rightbtn; //右边按钮
@propetry(natomoc,strong)UIImageView *myImageView; //显示图片
@propetry(natomoc,strong)NSArray *imagenames; //创建数组
@end
@implementation ViewController
-(void)viewDidlord{
[super viewDidLoad];
self.imagenames=@[@"biaoqingdi",@"bingli",@"chiniupa",@"dagteng",@"wangba"];
self.titlelabel=[UILabel alloc]initWithFrame:CGRectMake(180,30,150,30); //创建并初始化标签
self.titlelabel.text=@"biaoqingdi"; //给标签添加文本
[self.view.addSubview:self.titlelabel]; //添加标签至视图
self.leftbtn=[UILabel alloc]initWithFrame:CGRectMake(40,120,30,30,); //创建并初始化左按钮
UIimage *leftimage = [UIimage imageNamed:@"left_normal"]; //加载图片至内存
[self.leftbtn setBackgoundImage:leftimage forState:(UICtrolStateNormal)] //指定图片作为按钮
[self.view addSubview:self.leftbtn]; /添加左按钮至视图
self.myImageView=[[UIImageView alloc]initWithFrame:CGRectMake(110,60,200,200); //创建并初始化图片
UIImage *image=[UIImage imageNamed:@"biaoqingdi"]; //加载图片至内存
self.myImageView.image =image; //制定需添加图片
[self.view addSubview:self.myimageView]; //添加图片至视图
self.rightbtn=[UILabel alloc]initWithFrame:CGRectMake(3580,120,30,30); //创建并初始化右按钮
UIimage *rightimage = [UIimage imageNamed:@"right_normal"]; //加载图片至内存
[self.rightbtn setBackgoundImage:rightimage forState:(UICtrolStateNormal)] //指定图片作为按钮
[self.view addSubview:self.rightbtn]; /添加右按钮至视图
[self.leftbtn addtarget:self action:@selector(leftbtnAction)forContronlEvents;(UICtronlEventTouchUpside)]; //设置监听
}
-(void)rightbtnAction{
NSInteger index ={self.imagenames indexOfObject:self.titilelabel.text]; //创建整型变量作为数组下标
if (index <4 ) { //运行的条件
if (index==3){ //改变右边按钮的颜色和关闭交互
self.rightbtn.userInterractionEnabled= NO;
[UIImage *image =[UIImage imageNamed:@"right_disable"];
[self.rightbtn.userInteractionEnabled = NO;
UIImaged *image = [UIImage imagedNamed:@"RIGHT_disable"];
[self.rightbtn setBackgroundImage:image forState;(UIControlstateNormal)];
}else
{ //左边按钮和右边按钮都是在一个正常状态
self.leftBtn.UserInteractiionEnabled =YES;
self.rightBtn.UserInteractiionEnabled =YES;
UIImage *leftNormal =[UIImage imageNormal = [UIImage imageNamed:@"left_normal"];
UIImage *rightNormal =[UIImage imageNormal = [UIImage imageNamed:@"right_normal"];
[self.leftBtn setBackgroundImage: leftNormal forState:(UIControlStateNormal)];
[self.rightBtn setBackgroundImage: rightNormal forState:(UIControlStateNormal)];
}
NSString *nextTitle = self.imagenames[index + 1] //创建字符串代替数组中元素
self.titlelabel.text = nextTitle; //替换标签文本
self.myImageView.image = [UIImage imageNamed:nextTitle]; //替换图片
}
}
-(void)leftbtnAction
{
NSInteger index = [self.imagenames indexOfObject:self.titlelabel.text]; //创建整型变量做为数组下标
if(index>0) //运行条件
{ if(index == 1){ //左边按钮交互关闭,切换图片
self.leftbtn.userInteractionEnabled = NO;
UIImage *image = [UIImage imageNamed:@"left_disable"];
[self.leftbtn setBackgroundImage:image forState;(UIControlstateNormal)];
}else{
self.rightBtn.UserInteractiionEnabled =YES;
self.leftBtn.UserInteractiionEnabled =YES;
UIImage *leftNormal =[UIImage imageNormal = [UIImage imageNamed:@"left_normal"];
UIImage *rightNormal =[UIImage imageNormal = [UIImage imageNamed:@"right_normal"];
[self.leftBtn setBackgroundImage: leftNormal forState:(UIControlStateNormal)];
[self.rightBtn setBackgroundImage: rightNormal forState:(UIControlStateNormal)];
}
NSString *lastTitle = self.imagenames[index - 1] //创建字符串代替数组中元素
self.titlelabel.text = lastTitle; //替换标签文本
self.myImageView.image = [UIImage imageNamed:lastTitle]; //替换图片
}
-(void)didReceiveMemoryWarning{
[super didReceiveMemoryWarning];
//Dispose of any resources that can be recreated.
}
@end