#import"ViewController.h"
@interfaceViewController()
//標(biāo)題標(biāo)簽
@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"];
//創(chuàng)建并初始化標(biāo)簽文本
self.titlelabel=[[UILabelalloc]initWithFrame:CGRectMake(150,300,150,30)];
//設(shè)置標(biāo)簽文本
self.titlelabel.text=@"biaoqingdi";
//將標(biāo)簽添加到視圖
[self.viewaddSubview:self.titlelabel];
//創(chuàng)建并初始化左邊按鈕
self.leftBtn=[[UIButtonalloc]
initWithFrame:CGRectMake(20,150,45,45)];
//關(guān)閉交互
self.leftBtn.userInteractionEnabled=NO;
//根據(jù)名字加載圖片
UIImage*leftImage=[UIImageimageNamed:@"left_disable"];
//給左邊按鈕設(shè)置背景圖片
[self.leftBtnsetBackgroundImage:leftImageforState:
(UIControlStateNormal)];
//設(shè)置左邊按鈕顯示的圖片
[self.viewaddSubview:self.leftBtn];
//創(chuàng)建并初始化相框
self.myImageView=[[UIImageViewalloc]initWithFrame:CGRectMake(85,100,200,200)];
//根據(jù)名字加載圖片
UIImage*image=[UIImageimageNamed:@"biaoqingdi"];
self.myImageView.image=image;
//設(shè)置myImageView顯示的圖片
[self.viewaddSubview:self.myImageView];
//創(chuàng)建并初始化右邊按鈕
self.rightBtn=[[UIButtonalloc]initWithFrame:CGRectMake(305,150,45,45)];
//根據(jù)名字加載圖片
UIImage*rightImage=[UIImageimageNamed:@"right_normal"];
//設(shè)置右邊按鈕的背景圖片
[self.rightBtnsetBackgroundImage:rightImageforState:(UIControlStateNormal)];
//設(shè)置右邊按鈕顯示的圖片
[self.viewaddSubview:self.rightBtn];
[self.rightBtnaddTarget:selfaction:@selector(rightBtnAction)forControlEvents:(UIControlEventTouchUpInside)];
[self.leftBtnaddTarget:selfaction:@selector(leftBtnAction)forControlEvents:(UIControlEventTouchUpInside)];
}
-(void)rightBtnAction
{
//切換到下一張圖片
//獲取當(dāng)前是第幾張圖片
NSIntegerindex=[self.imageNamesindexOfObject:self.titlelabel.text];
//不是為最后一張才切換到最后一張
if(index<4){
if(index==3){
//改變右邊按鈕顏色和關(guān)閉交互
self.rightBtn.userInteractionEnabled=NO;
UIImage*image=[UIImageimageNamed:@"right_disable"];
[self.rightBtnsetBackgroundImage:imageforState:(UIControlStateNormal)];
}else{
//左邊按鈕和右邊按鈕都是在一個(gè)正常狀態(tài)
self.leftBtn.userInteractionEnabled=YES;
UIImage*leftNormal=[UIImageimageNamed:@"left_normal"];
UIImage*rightNormal=[UIImageimageNamed:@"right_normal"];
[self.leftBtnsetBackgroundImage:leftNormalforState:(UIControlStateNormal)];
[self.rightBtnsetBackgroundImage:rightNormalforState:(UIControlStateNormal)];
}
//通過下標(biāo)訪問下一張圖片
NSString*nextTitle=self.imageNames[index+1];
self.titlelabel.text=nextTitle;
//設(shè)置相框顯示的下一張圖片
self.myImageView.image=[UIImageimageNamed:nextTitle];
}
}
-(void)leftBtnAction
{
NSIntegerindex=[self.imageNamesindexOfObject:self.titlelabel.text];
//不是第一張的時(shí)候才切換到上一張
if(index>0){
if(index==1){
//左邊按鈕交互關(guān)閉,圖片切換
self.leftBtn.userInteractionEnabled=NO;
UIImage*image=[UIImageimageNamed:@"left_disable"];
[self.leftBtnsetBackgroundImage:imageforState:(+UIControlStateNormal)];
}else{
//左右兩邊按鈕都是正常狀態(tài)
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??'?_?