UI控件

//1.按鈕UIButton

UIButton *button //表示定義了一個(gè)按鈕對(duì)象

button.frame=CGMake(x軸,y軸,寬,高)//(frame設(shè)置控件的位置)

//設(shè)置按鈕上的文本

[button setTitle:@" 文本內(nèi)容" forState:按鈕狀態(tài)];

//設(shè)置按鈕背景顏色

button.BackgroundColor=[UIColor 顏色]

//按鈕的監(jiān)聽

[button addTaget:self action:@selector(btnClicklister) forControlEvents:按鈕的控制狀態(tài)]

//監(jiān)聽按鈕的結(jié)果

-(void)btnClickEvents{

//按鈕所具有的功能

}

//圖片UIImage

//根據(jù)名稱加載圖片

UIImage *image=[UIImage imageNamed:@"圖片名稱"]

//設(shè)置按鈕的背景圖片

[button setBackgroundImage:image forState:按鈕狀態(tài)]

//相框UIImageView

UIImageView *imageview=[UIImageView alloc]initWithFrame:CGMake(x軸,y軸,寬,高)

//標(biāo)簽UILabel

//初始化標(biāo)簽并設(shè)置其位置

UILabel *label = [UILabel alloc]initWithFrame:CGMake:(x軸,y軸,寬,高)]

//設(shè)置標(biāo)簽的文本

label.text=@"標(biāo)簽文本內(nèi)容"

//設(shè)置標(biāo)簽相對(duì)居中方式

label.textAlignment=NSTextAlignmentsCenter

//把控件添加到視圖中去

[self.view addSubview:添加的控件]

//設(shè)置標(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

@implementation ViewController

- (void)viewDidLoad {

? ?[super viewDidLoad];

? ?//定義一個(gè)數(shù)組來(lái)存放圖片

? ?self.imagenames = @[@"biaoqingdi",@"bingli",@"chiniupa",@"danteng",@"wangba"];

? ?//設(shè)置標(biāo)簽的位置

? ?self.titlelabel=[[UILabel alloc]initWithFrame:CGRectMake(50, 50, 150, 30)];

? ?//設(shè)置標(biāo)簽的內(nèi)容

? ?self.titlelabel.text = @"biaoqingdi";

? ?self.titlelabel.textAlignment=NSTextAlignmentCenter;

? ?//把標(biāo)簽添加到視圖中去

? ?[self.view addSubview:self.titlelabel];

? ?//設(shè)置左邊按鈕的位置

? ?self.leftbtn=[[UIButton alloc]initWithFrame:CGRectMake(20, 150, 45, 45)];

? ?//關(guān)閉交互

? ?self.leftbtn.userInteractionEnabled=NO;

? ?//根據(jù)名字加載圖片

? ?UIImage *leftimage = [UIImage imageNamed:@"left_disable"];

? ?//設(shè)置按鈕的狀態(tài)和背景圖片

? ?[self.leftbtn setBackgroundImage:leftimage forState:(UIControlStateNormal)];

? ?//對(duì)按鈕進(jìn)行監(jiān)聽

? ?[self.leftbtn addTarget:self action:@selector(leftbtnAction) forControlEvents:(UIControlEventTouchUpInside)];

? ? ?//把按鈕添加到視圖中

? ?[self.view addSubview:self.leftbtn];

? ?//設(shè)置圖片的位置

? ?self.myimageview=[[UIImageView alloc]initWithFrame:CGRectMake(80, 100, 200, 200)];

? ?//根據(jù)名字加載圖片

? ?UIImage *image=[UIImage imageNamed:@"biaoqingdi"];

? ?//調(diào)用圖片

? ?self.myimageview.image=image;

? ?//把圖片添加到視圖中

? ?[self.view addSubview:self.myimageview];

? ?//設(shè)置右邊按鈕的位置

? ?self.rightbtn=[[UIButton alloc]initWithFrame:CGRectMake(305, 150, 45, 45)];

? ?//根據(jù)名字加載圖片

? ?UIImage *rightimage=[UIImage imageNamed:@"right_normal"];

? ?//設(shè)置按鈕的背景圖片和狀態(tài)

? ?[self.rightbtn setBackgroundImage:rightimage forState:(UIControlStateNormal)];

? ?//把按鈕添加到視圖中

? ?[self.view addSubview:self.rightbtn];

? ?[self.rightbtn addTarget:self action:@selector(rightbtnAction) forControlEvents:(UIControlEventTouchUpInside)];

? }

-(void)rightbtnAction{

? ? //切換到下一張圖片

? ?//獲取當(dāng)前是第幾張圖片

? ?NSInteger index=[self.imagenames indexOfObject:self.titlelabel.text];

? ?//不是最后一張才切換到下一張

? ?if(index<4){

? ? ? ?if(index==3){

? ? ? ? ? ?//改變右邊按鈕的顏色和關(guān)閉交互

? ? ? ? ? ?self.rightbtn.userInteractionEnabled=NO;

? ? ? ? ? ?//根據(jù)名稱加載圖片

? ? ? ? ? ?UIImage *image=[UIImage imageNamed:@"right_disable"];

? ? ? ? ? ?//設(shè)置按鈕的背景圖片

? ? ? ? ? ?[self.rightbtn setBackgroundImage:image forState:(UIControlStateNormal)];

? ? ? ?}else{

? ? ? ? ? ?//左邊和右邊按鈕都是在一個(gè)正常狀態(tài)

? ? ? ? ? ?self.leftbtn.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)];

? ? ? ?}

? ? ? ?self.leftbtn.userInteractionEnabled=YES;

? ? ? ?NSString ?*nextTitle=self.imagenames[index+1];

? ? ? ?self.titlelabel.text=nextTitle;

? ? ? ?//self.titlelabel.text=self.imagenames[index+1];

? ? ? ?self.myimageview.image=[UIImage imageNamed:nextTitle];

? ?}

}

-(void)leftbtnAction{

? ?NSInteger index=[self.imagenames indexOfObject:self.titlelabel.text];

? ?//不是最后一張才切換到下一張

? ?if(index>0){

? ? ? ?if(index==1){

? ? ? ? ? ?//左邊按鈕交互關(guān)閉,圖片切換

? ? ? ? ? ?self.leftbtn.userInteractionEnabled=NO;

? ? ? ? ? ?UIImage *image=[UIImage imageNamed:@"left_disable"];

? ? ? ? ? ?[self.leftbtn setBackgroundImage:image forState:(UIControlStateNormal)];

? ? ? ?}else{

? ? ? ? ? ?//左邊和右邊按鈕都是在一個(gè)正常狀態(tài)

? ? ? ? ? ?self.leftbtn.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.titlelabel.text=self.imagenames[index+1];

? ? ? ?self.myimageview.image=[UIImage imageNamed:nextTitle];

? ?}

}

-(void)btnClickLister{

? ?NSLog(@"CLICK BUTTON");

}

-(void)Demo{

? ?// 按鈕UIBotton

? ?// ? ?UIButton *button =[UIButton buttonWithType:UIButtonTypeContactAdd];

? ?UIButton *button =[[UIButton alloc]initWithFrame:CGRectMake(50, 50, 80, 80)];

? ?//frame表示控件的坐標(biāo)和寬高(CGrect類型)

? ?// ? ?button.frame = CGRectMake(50, 50, 80, 80);

? ?//設(shè)置文本

? ?// ? ?[button setTitle:@"yang" forState: UIControlStateNormal];

? ?//根據(jù)名字加載圖片

? ?UIImage *image = [UIImage imageNamed:@"right_normal"];

? ?//給按鈕設(shè)置背景圖片

? ?[button setBackgroundImage:image forState: UIControlStateNormal];

? ?button.backgroundColor = [UIColor redColor];

? ?//按鈕的監(jiān)聽

? ?[button addTarget:self action:@selector(btnClickLister) forControlEvents:UIControlEventTouchUpOutside ?];

? ?UIImage *image1=[UIImage imageNamed:@"biaoqingdi"];

? ?//添加到視圖上

? ?[self.view addSubview:button];

? ?//相框UIImageView

? ?UIImageView *imageview = [[UIImageView alloc]initWithFrame:CGRectMake(150, 50, 200, 200)];

? ?//設(shè)置imageview顯示的圖片

? ?imageview.image = image1;

? ?[self.view addSubview:imageview];

? ?//標(biāo)簽UILabel

? ?UILabel *label =[[UILabel alloc]initWithFrame:CGRectMake(150, 270, 150, 30)];

? ?//設(shè)置標(biāo)簽的文本

? ?label.text=@"表情帝";

? ?//設(shè)置居中方式

? ?label.textAlignment=NSTextAlignmentCenter;

? ?label.textColor=[UIColor redColor];

? ?[self.view addSubview:label];

}

- (void)didReceiveMemoryWarning {

? ?[super didReceiveMemoryWarning];

? ?// Dispose of any resources that can be recreated.

}

@end

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

推薦閱讀更多精彩內(nèi)容

  • ViewController.m // UI常用控件 // // Created by lanou on 16/7...
    Teamo5閱讀 169評(píng)論 0 0
  • ViewController.m // UI常用控件 // // Created by lanou on 16/7...
    一起長(zhǎng)大的小曖昧閱讀 187評(píng)論 0 0
  • // //ViewController.m //UI常用控件 // //Created bylanou on 16...
    貝貝貝1010閱讀 163評(píng)論 0 0
  • 站在二樓的一個(gè)窗口往外望,左邊是剛漏出緋紅的陽(yáng)光,右邊是樓下生活部的隊(duì)列。我決定還是洗個(gè)臉去吃飯。
    苧麻尼閱讀 130評(píng)論 0 1
  • 人活在世上是一種巧合也是一種必然,在巧合與必然中誕生了你,那么你該慶幸這造物主的垂愛,在父母親偶爾一次的歡娛中成就...
    青璇小憩閱讀 650評(píng)論 3 3