//标签按钮
@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
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.imageNames = @[@"biaoqingdi",@"bingli",@"chiniupa",@"danteng",@"wangba"];
// 创建并初始化标签
self.titleLabel = [[UILabel alloc]initWithFrame:CGRectMake(165, 60, 150, 30)];
// 设置文本
self.titleLabel.text = @"biaoqingdi";
self.titleLabel.textColor= [UIColor grayColor];
// 设置显示在视图上
[self.view addSubview:self.titleLabel];
// 创建并初始化左边按钮
self.leftBtn = [[UIButton alloc]initWithFrame:CGRectMake(20, 150, 45, 45)];
// 关闭交互
self.leftBtn.userInteractionEnabled = NO;
// 添加图片
UIImage *leftImage = [UIImage imageNamed:@"left_disable"];
// 给按钮添加背景图片
[self.leftBtn setBackgroundImage:leftImage forState: (UIControlStateNormal)];
// 设置显示在视图上
[self.view addSubview:self.leftBtn];
[self.leftBtn addTarget:self action:@selector(leftBtnAction) forControlEvents:(UIControlEventTouchUpInside)];
// 创建并初始化相框
self.myImageView = [[UIImageView alloc]initWithFrame:CGRectMake(105, 100, 200, 200)];
// 添加图片
UIImage *image = [UIImage imageNamed:@"biaoqingdi"];
// 将图片添加到相框中
self.myImageView.image = image;
// 设置显示在视图上
[self.view addSubview:self.myImageView];
// 创建并初始化右边按钮
self.rightBtn = [[UIButton alloc]initWithFrame:CGRectMake(345, 150, 45, 45)];
// 添加图片
UIImage *rightImage = [UIImage imageNamed:@"right_normal"];
// 为按钮设置背景图片
[self.rightBtn setBackgroundImage:rightImage forState: (UIControlStateNormal)];
// 设置显示在视图上
[self.view addSubview:self.rightBtn];
[self.rightBtn addTarget:self action:@selector(rightBtnAction) forControlEvents:(UIControlEventTouchUpInside)];
}
-(void)rightBtnAction{
// 切换到下一张图片
// 获取当前是第几张图片
NSInteger index = [self.imageNames indexOfObject:self.titleLabel.text];
// 不是最后一张才切换到下一张
if(index < 4){
if(index == 3){
// 改变右边按钮颜色和关闭交互
self.rightBtn.userInteractionEnabled = NO;
UIImage *image = [UIImage imageNamed:@"right_disable"];
[self.rightBtn setBackgroundImage:image forState:(UIControlStateNormal)];
}
else{
// 左边按钮和右边按钮都是在一个正常状态
self.leftBtn.userInteractionEnabled = YES;
self.rightBtn.userInteractionEnabled = YES;
UIImage *leftNormal = [UIImage imageNamed:@"left_normal"];
UIImage *rightNormal = [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.leftBtn.userInteractionEnabled = YES;
self.rightBtn.userInteractionEnabled = YES;
UIImage *leftNormal = [UIImage imageNamed:@"left_normal"];
UIImage *rightNormal = [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