//
//ViewController.m
//UI常用控件
//
//Created bylanou on 16/7/10.
//Copyright?2016年lanou. All rights reserved.
//
#import"ViewController.h"
@interfaceViewController ()
//標題標簽
@property(nonatomic,strong)UILabel*titleLabel;
//左邊按鈕
@property(nonatomic,strong)UIButton*leftBtn;
//右邊按鈕
@property(nonatomic,strong)UIButton*rightBtn;
//顯示圖片
@property(nonatomic,strong)UIImageView*myImageView;
//定義數(shù)組名
@property(nonatomic,strong)NSArray*imageNames;
@end
@implementationViewController
- (void)viewDidLoad {
[superviewDidLoad];
self.imageNames=@[@"biaoqingdi",@"bingli",@"chiniupa",@"danteng",@"wangba"];
//定義標簽位置與名稱
self.titleLabel= [[UILabelalloc]initWithFrame:CGRectMake(150,50,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:leftImage
forState:(UIControlStateNormal)];
[self.viewaddSubview:self.leftBtn];
//顯示相框名稱
self.myImageView= [[UIImageViewalloc]initWithFrame:CGRectMake(85,100,200,200)];
UIImage*image = [UIImageimageNamed:@"biaoqingdi"];
self.myImageView.image= image;
//顯示相框圖片
[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];
//按鈕的監(jiān)聽
[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:image forState:(UIControlStateNormal)];
}else{
//左邊按鈕和右邊按鈕都是在一個正常狀態(tài)
self.leftBtn.userInteractionEnabled=YES;
self.rightBtn.userInteractionEnabled=YES;
UIImage*leftNormal = [UIImageimageNamed:@"left_normal"];
UIImage*rightNormal =[UIImageimageNamed:@"right_normal"];
[self.leftBtnsetBackgroundImage:leftNormal
forState:(UIControlStateNormal)];
[self.rightBtnsetBackgroundImage:rightNormal
forState:(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:image forState:(UIControlStateNormal)];
}else{
//左邊按鈕和右邊按鈕都是在一個正常狀態(tài)
self.leftBtn.userInteractionEnabled=YES;
self.rightBtn.userInteractionEnabled=YES;
UIImage*leftNormal = [UIImageimageNamed:@"left_normal"];
UIImage*rightNormal =[UIImageimageNamed:@"right_normal"];
[self.leftBtnsetBackgroundImage:leftNormal
forState:(UIControlStateNormal)];
[self.rightBtnsetBackgroundImage:rightNormal
forState:(UIControlStateNormal)];
}
NSString*preTitle =self.imageNames[index-1];
self.titleLabel.text= preTitle;
self.myImageView.image= [UIImage imageNamed:preTitle];
}
}
-(void)btnClickLister
{
NSLog(@"click
btn");
}
-(void)demo {
//UIView *view =[u]
//按鈕UIButton
//UIButton *button =[UIButton buttonWithType:UIButtonTypeInfoDark];
UIButton*button = [[UIButtonalloc]initWithFrame:CGRectMake(20,50,80,80)];
//frame表明了控件的坐標和寬高(CGRect類型)
//[button setTitle:@"阿陶" forState:UIControlStateNormal];
UIImage*image = [UIImageimageNamed:@"left_normal"];
//根據(jù)名字加載圖片
[buttonsetBackgroundImage:image forState:UIControlStateNormal];
//給按鈕設置背景圖片
//button.backgroundColor = [UIColor redColor];
//按鈕的監(jiān)聽
[buttonaddTarget:selfaction:@selector(btnClickLister)
forControlEvents:UIControlEventTouchUpInside];
//添加到視圖上面
[self.viewaddSubview:button];
//相框UIImageView
UIImageView*imageview = [[UIImageViewalloc]initWithFrame:CGRectMake(150,50,200,200)];
UIImage*image1 = [UIImageimageNamed:@"biaoqingdi"];
//設置imageView顯示的圖片
imageview.image= image1;
[self.viewaddSubview:imageview];
//標簽UILabel
UILabel*label = [[UILabelalloc]initWithFrame:CGRectMake(150,270,150,30)];
//設置標簽文本
label.text=@"濤哥哥";
//設置居中方式
label.textAlignment=NSTextAlignmentCenter;
label.textColor= [UIColorredColor];
[self.viewaddSubview:label];}
@end