UI常用控件
//
// ?ViewController.m
// ?UI常用控件
//
// ?Created by lanou on 16/7/10.
// ?Copyright?2016年lanou. All rights reserved.
//
#import"ViewController.h"
@interfaceViewController()
//標(biāo)題標(biāo)簽
@property(nonatomic,strong)UILabel*titleLable;
//左邊按鈕
@property(nonatomic,strong)UIButton*leftBtn;
//右邊按鈕
@property(nonatomic,strong)UIButton*rightBtn;
//顯示圖片
@property(nonatomic,strong)UIImageView*myImageView;
@end
@implementationViewController
- (void)viewDidLoad {
[superviewDidLoad];
//定義標(biāo)簽位置與名稱 ?創(chuàng)建并初始化標(biāo)簽
self.titleLable= [[UILabelalloc]initWithFrame:CGRectMake(150,50,150,30)];
注:frame表示控件的坐標(biāo)以及寬高
//設(shè)置標(biāo)簽文本
self.titleLable.text=@"biaoqingdi";
[self.viewaddSubview:self.titleLable];
//定義做按鈕的位置
self.leftBtn= [[UIButtonalloc]initWithFrame:CGRectMake(20,150,45,45)];
//定義按鈕的圖片 ?根據(jù)名字加載圖片
UIImage *leftImage = [UIImageimageNamed:@"left_normal"];
//設(shè)置左按鈕的背景圖片
[self.leftBtnsetBackgroundImage:leftImageforState:(UIControlStateNormal)];
//添加到視圖上面
[self.viewaddSubview:self.leftBtn]; ? ?
//顯示相框名稱 ?設(shè)置imageview顯示的圖片
self.myImageView= [[UIImageView alloc]initWithFrame:CGRectMake(85,100,200,200)];
UIImage *image = [UIImageimageNamed:@"biaoqingdi"];
self.myImageView.image= image;
//顯示相框圖片
[self.viewaddSubview:self.myImageView];
//設(shè)置右按鈕的位置
self.rightBtn=[[UIButtonalloc]initWithFrame:CGRectMake(305,150,45,45)];
//設(shè)置右按鈕的圖片
UIImage *rightImage = [UIImageimageNamed:@"right_normal"];
//設(shè)置右按鈕的背景圖片
[self.rightBtnsetBackgroundImage:rightImageforState:(UIControlStateNormal)];
[self.viewaddSubview:self.rightBtn];
}
-(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表明了控件的坐標(biāo)和寬高(CGRect類型)
// ???[button setTitle:@"柚子" forState:UIControlStateNormal];
UIImage *image = [UIImageimageNamed:@"left_normal"];
//根據(jù)名字加載圖片
[buttonsetBackgroundImage:imageforState:UIControlStateNormal];
//給按鈕設(shè)置背景圖片
// ??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"];
//設(shè)置imageView顯示的圖片
imageview.image= image1;
[self.viewaddSubview:imageview];
//標(biāo)簽UILabel
UILabel*label = [[UILabelalloc]initWithFrame:CGRectMake(150,270,150,30)];
//設(shè)置標(biāo)簽文本
label.text=@"濤哥哥";
//設(shè)置居中方式
label.textAlignment=NSTextAlignmentCenter;
label.textColor= [UIColorredColor];
[self.viewaddSubview:label];}
@end
//
// ?ViewController.m
// ?UI常用控件
//
// ?Created by lanou on 16/7/10.
// ?Copyright?2016年lanou. All rights reserved.
//
#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;
//定義數(shù)組名
@property(nonatomic,strong)NSArray*imageNames;
@end
@implementationViewController
- (void)viewDidLoad {
[superviewDidLoad];
self.imageNames=@[@"biaoqingdi",@"bingli",@"chiniupa",@"danteng",@"wangba"];
//定義標(biāo)簽位置與名稱
self.titleLabel= [[UILabelalloc]initWithFrame:CGRectMake(150,50,150,30)];
// 設(shè)置標(biāo)簽文本
self.titleLabel.text=@"biaoqingdi";
[self.viewaddSubview:self.titleLabel];
//定義做按鈕的位置
self.leftBtn= [[UIButtonalloc]initWithFrame:CGRectMake(20,150,45,45)];
//關(guān)閉交互 ?BOOL類型
self.leftBtn.userInteractionEnabled=NO;
//定義按鈕的圖片
UIImage*leftImage = [UIImageimageNamed:@"left_disable"];
//設(shè)置左按鈕的背景圖片
[self.leftBtnsetBackgroundImage:leftImage forState:(UIControlStateNormal)];
[self.viewaddSubview:self.leftBtn];
//顯示相框名稱
self.myImageView= [[UIImageView alloc]initWithFrame:CGRectMake(85,100,200,200)];
UIImage *image = [UIImageimageNamed:@"biaoqingdi"];
self.myImageView.image= image;
//顯示相框圖片
[self.viewaddSubview:self.myImageView];
//設(shè)置右按鈕的位置
self.rightBtn=[[UIButtonalloc]initWithFrame:CGRectMake(305,150,45,45)];
//設(shè)置右按鈕的圖片 ? 根據(jù)名字加載圖片
UIImage *rightImage = [UIImageimageNamed:@"right_normal"];
//設(shè)置右按鈕的背景圖片
[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
{
//切換到下一張圖片
//獲取當(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:image forState:(UIControlStateNormal)];
}else{
//左邊按鈕和右邊按鈕都是在一個(gè)正常狀態(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){
//改變左邊按鈕的圖片和關(guān)閉交互
self.leftBtn.userInteractionEnabled=NO;
UIImage*image = [UIImageimageNamed:@"left_disable"];
[self.leftBtnsetBackgroundImage:image forState:(UIControlStateNormal)];
}else{
//左邊按鈕和右邊按鈕都是在一個(gè)正常狀態(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表明了控件的坐標(biāo)和寬高(CGRect類型)
// ???[button setTitle:@"阿陶" forState:UIControlStateNormal];
UIImage*image = [UIImageimageNamed:@"left_normal"];
//根據(jù)名字加載圖片
[buttonsetBackgroundImage:image forState:UIControlStateNormal];
//給按鈕設(shè)置背景圖片
// ??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"];
//設(shè)置imageView顯示的圖片
imageview.image= image1;
[self.viewaddSubview:imageview];
//標(biāo)簽UILabel
UILabel*label = [[UILabelalloc]initWithFrame:CGRectMake(150,270,150,30)];
//設(shè)置標(biāo)簽文本
label.text=@"濤哥哥";
//設(shè)置居中方式
label.textAlignment=NSTextAlignmentCenter;
label.textColor= [UIColorredColor];
[self.view addSubview:label];}
@end