2016/07/11
//
// ?ViewController.m
// ?UI按鈕交互功能
//
// ?Created by lanou on 16/7/11.
// ?Copyright ? 2016年yhy. All rights reserved.
//
#import "ViewController.h"
@interface ViewController ()
//標題標簽
@property(nonatomic,strong)UILabel *titleLable;
//左邊按鈕
@property(nonatomic,strong)UIButton *leftBtn;
//右邊按鈕
@property(nonatomic,strong)UIButton *rightBtn;
//顯示圖片
@property(nonatomic,strong)UIImageView *myImageView;
//給存入的四張圖片定義一個數(shù)組
@property(nonatomic,strong)NSArray *imageNames;
@end
@implementation ViewController
//視圖加載好時會自動調用
- (void)viewDidLoad {
[super viewDidLoad];
//創(chuàng)建并初始化標簽
self.titleLable = [[UILabel alloc]initWithFrame:CGRectMake(100, 50, 150, 30)];
//寫入標簽文本
self.titleLable.text = @"biaoqingdi";
//設置標簽文本的背景顏色為紅色
self.titleLable.backgroundColor = [UIColor greenColor];
//設置標簽文本在框中居中對齊
self.titleLable.textAlignment = NSTextAlignmentCenter;
//添加標簽到視圖上
[self.view addSubview:self.titleLable];
//創(chuàng)建并初始化定義(坐標为狸、寬高)相框
self.myImageView = [[UIImageView alloc]initWithFrame:CGRectMake(85, 100, 200, 200)];
//將圖片保存在名為image中(根據(jù)圖片地址名加載圖片)
UIImage *image = [UIImage imageNamed:@"biaoqingdi"];
//顯示圖片
self.myImageView.image = image;
//添加相框到視圖中
[self.view addSubview:_myImageView];
//創(chuàng)建并初始化定義(坐標突想、寬高)左按鈕
self.leftBtn = [[UIButton alloc]initWithFrame:CGRectMake(20, 150, 45, 45)];
//關閉左按鈕的交互
self.leftBtn.userInteractionEnabled = NO;
//將左按鈕的圖片保存在名為leftImage中(根據(jù)圖片地址名加載圖片)
UIImage *leftImage = [UIImage imageNamed: @"left_disable"];
//設置左按鈕的背景圖片(在正常狀態(tài)下)
[self.leftBtn setBackgroundImage:leftImage forState:(UIControlStateNormal)];
//添加左按鈕到視圖上
[self.view addSubview: _leftBtn];
//創(chuàng)建并初始化定義(坐標、寬高)右按鈕
self.rightBtn = [[UIButton alloc]initWithFrame:CGRectMake(305, 150, 45, 45)];
//將右按鈕的圖片保存在名為rightImage中(根據(jù)圖片地址名加載圖片)
UIImage *rightImage = [UIImage imageNamed: @"right_normal"];
//設置右按鈕的背景圖片(在正常狀態(tài)下)
[self.rightBtn setBackgroundImage:rightImage forState:(UIControlStateNormal)];
//添加右按鈕在視圖上
[self.view addSubview:_rightBtn];
//右按鈕監(jiān)聽
[self.rightBtn addTarget:self action:@selector(rightBtnAction) forControlEvents:(UIControlEventTouchUpInside)];
//左按鈕監(jiān)聽
[self.leftBtn addTarget:self action:@selector(leftBtnAction) forControlEvents:(UIControlEventTouchUpInside)];
}
//右按鈕
-(void)rightBtnAction{
//切換圖片 ? 圖片為png格式則不用加后綴名 其他格式需要加后綴
self.imageNames = @[ @"biaoqingdi",@"bingli",@"chiniupa",@"danteng",@"wangba"];
//獲取當前是第幾張圖片(整型變量)
NSInteger index = [self.imageNames indexOfObject:self.titleLable.text];
//把按鈕圖片加載成灰色(正常狀態(tài)下狀態(tài))
//不是為最后一張才切換到下一張
if (index < 4) {
//如果index=3(即圖片從第四張切換到雹仿,第五張時)
if (index == 3) {
//關閉右按鈕交互(右按鈕圖片變?yōu)榛疑?/p>
self.rightBtn.userInteractionEnabled = NO;
//將右按鈕灰色圖片保存在image中(根據(jù)名字加載圖片)
UIImage *image =[UIImage imageNamed:@"right_disable"];
//將右按鈕狀態(tài)為一般(正常)狀態(tài)下的背景圖片設置成為image中圖像
[self.rightBtn setBackgroundImage:image forState:(UIControlStateNormal)];
}
else{
//否則(圖片為第1,2,3張時)
//將兩個按鈕正常狀態(tài)下的背景圖片設置為黑色圖片
[self.rightBtn setBackgroundImage:[UIImage imageNamed:@"right_normal"] forState:(UIControlStateNormal)];
[self.leftBtn setBackgroundImage:[UIImage imageNamed:@"left_normal"] forState:(UIControlStateNormal)];
//開啟兩個按鈕的交互
self.rightBtn.userInteractionEnabled = YES;
self.leftBtn.userInteractionEnabled = YES;
}
//將標簽titleLable內容切換到下一張圖片的標簽內容
NSString *nextTitle = self.imageNames[index+1];
self.titleLable.text = nextTitle;
//將圖片切換到下一張
self.myImageView.image = [UIImage imageNamed: nextTitle];
}
}
//左按鈕
-(void)leftBtnAction{
//切換圖片 ? 圖片為png格式則不用加后綴名 其他格式需要加后綴
self.imageNames = @[ @"biaoqingdi",@"bingli",@"chiniupa",@"danteng",@"wangba"];
//獲取當前是第幾張圖片(整型)
NSInteger index = [self.imageNames indexOfObject:self.titleLable.text];
//不是為第一張才切換到上一張
if (index > 0) {
//第二張切換到第一張時
if (index == 1) {
//左邊按鈕交互關閉(左按鈕圖片變?yōu)榛疑?/p>
self.leftBtn.userInteractionEnabled = NO;
//將左按鈕灰色圖片保存在image中(根據(jù)名字加載圖片)
UIImage *image =[UIImage imageNamed:@"left_disable"];
//將左按鈕狀態(tài)為一般(正常)狀態(tài)下的背景圖片設置成為image中圖像
[self.leftBtn setBackgroundImage:image forState:(UIControlStateNormal)];
}
else{
//否則(圖片為第3,4,5張時)
//將兩個按鈕正常狀態(tài)下的背景圖片設置為黑色圖片
[self.rightBtn setBackgroundImage:[UIImage imageNamed:@"right_normal"] forState:(UIControlStateNormal)];
[self.leftBtn setBackgroundImage:[UIImage imageNamed:@"left_normal"] forState:(UIControlStateNormal)];
//開啟兩個按鈕的交互
self.rightBtn.userInteractionEnabled = YES;
self.leftBtn.userInteractionEnabled = YES;
}
//將標簽titleLable內容切換到上一張圖片的標簽內容
NSString *preTitle =self.imageNames[index-1];
self.titleLable.text = preTitle;
//將圖片切換到上一張
self.myImageView.image = [UIImage imageNamed: preTitle];
}
}
-(void)demo{
//按鈕UIButton
// ? ?UIButton *button = [UIButton buttonWithType: UIButtonTypeContactAdd];
//創(chuàng)建并初始化定義(坐標、寬高)按鈕
UIButton *button = [[UIButton alloc]initWithFrame:CGRectMake(50, 50, 80, 80)];
// ? ? ?frame表明了空間的坐標和寬高(CGRect類型)
[button setTitle:@"眼鏡哥" forState:UIControlEventTouchDown];
//設置按鈕背景色為紅色
button.backgroundColor = [UIColor redColor];
//根據(jù)名字加載圖片
UIImage *image = [UIImage imageNamed:@"right_normal"];
//給按鈕設置背景圖片
[button setBackgroundImage:image forState:UIControlStateNormal];
//按鈕的監(jiān)聽
[button addTarget:self action:@selector(btnClickLister) forControlEvents:UIControlEventTouchUpOutside];
//添加到屏幕上面
[self.view addSubview:button];
//相框UIImageView
//創(chuàng)建并初始化視圖相框
UIImageView *imageView = [[UIImageView alloc]initWithFrame:CGRectMake(150, 50, 200, 200)];
UIImage *image1 = [UIImage imageNamed:@"biaoqingdi"];
//設置imageView顯示的圖片
imageView.image = image1;
[self.view addSubview:imageView];
//標簽UIlable
UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(150, 270, 150, 30)];
//設置標簽的文本
label.text = @"表情弟";
//設置居中方式(注:是將文本在標簽框內部居中)
label.textAlignment = NSTextAlignmentCenter;
//設置標簽的顏色為紅色
label.textColor = [UIColor redColor];
//將標簽顯示在視圖屏幕上
[self.view addSubview:label];
}
//方法調用
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
}
//調用按鈕監(jiān)聽的方法
-(void)btnClickLister{
//在視圖屏幕中執(zhí)行按鈕操作時竟痰,代碼工程中將會打印“click btn”
NSLog(@"click btn");
}
@end