UI控件

//1.按钮UIButton

UIButton *button //表示定义了一个按钮对象

button.frame=CGMake(x轴,y轴,宽,高)//(frame设置控件的位置)

//设置按钮上的文本

[button setTitle:@" 文本内容" forState:按钮状态];

//设置按钮背景颜色

button.BackgroundColor=[UIColor 颜色]

//按钮的监听

[button addTaget:self action:@selector(btnClicklister) forControlEvents:按钮的控制状态]

//监听按钮的结果

-(void)btnClickEvents{

//按钮所具有的功能

}

//图片UIImage

//根据名称加载图片

UIImage *image=[UIImage imageNamed:@"图片名称"]

//设置按钮的背景图片

[button setBackgroundImage:image forState:按钮状态]

//相框UIImageView

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

//标签UILabel

//初始化标签并设置其位置

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

//设置标签的文本

label.text=@"标签文本内容"

//设置标签相对居中方式

label.textAlignment=NSTextAlignmentsCenter

//把控件添加到视图中去

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

//设置标签

@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(50, 50, 150, 30)];

   //设置标签的内容

   self.titlelabel.text = @"biaoqingdi";

   self.titlelabel.textAlignment=NSTextAlignmentCenter;

   //把标签添加到视图中去

   [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.leftbtn addTarget:self action:@selector(leftbtnAction) forControlEvents:(UIControlEventTouchUpInside)];

     //把按钮添加到视图中

   [self.view addSubview:self.leftbtn];

   //设置图片的位置

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

   //根据名字加载图片

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

   //调用图片

   self.myimageview.image=image;

   //把图片添加到视图中

   [self.view addSubview:self.myimageview];

   //设置右边按钮的位置

   self.rightbtn=[[UIButton alloc]initWithFrame:CGRectMake(305, 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;

           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){

           //左边按钮交互关闭,图片切换

           self.leftbtn.userInteractionEnabled=NO;

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

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

       }else{

           //左边和右边按钮都是在一个正常状态

           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表示控件的坐标和宽高(CGrect类型)

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

   //设置文本

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

   //根据名字加载图片

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

   //给按钮设置背景图片

   [button setBackgroundImage:image forState: UIControlStateNormal];

   button.backgroundColor = [UIColor redColor];

   //按钮的监听

   [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)];

   //设置imageview显示的图片

   imageview.image = image1;

   [self.view addSubview:imageview];

   //标签UILabel

   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];

   // Dispose of any resources that can be recreated.

}

@end

你可能感兴趣的:(UI控件)