IOS(UI)_UIButton(按钮)和UIImageView(图片视图)

时间:2021-06-18 21:06:39


图片

Off.png

IOS(UI)_UIButton(按钮)和UIImageView(图片视图)


On.png


IOS(UI)_UIButton(按钮)和UIImageView(图片视图)


speakPhone.png

IOS(UI)_UIButton(按钮)和UIImageView(图片视图)

speak_one.png

IOS(UI)_UIButton(按钮)和UIImageView(图片视图)

speak_two.png


IOS(UI)_UIButton(按钮)和UIImageView(图片视图)

========================================================


#import "ViewController.h"


@interface ViewController ()

{


     //定义全局控件

    UIImageView *imageViewTwo;

}



@end


@implementation ViewController


- (void)viewDidLoad {

    [superviewDidLoad];

    /*

    UILabel *label=[[UILabel alloc] initWithFrame:CGRectZero];

    label.text=@"";

    CGRect rect=[label.text boundingRectWithSize:CGSizeMake(300,CGFLOAT_MAX) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:label.font} context:nil];

    label.frame=CGRectMake(0, 20, 300, rect.size.height);

    label.numberOfLines=0;

    label.backgroundColor=[UIColor cyanColor];

    [self.view addSubview:label];

    */

    //UIButton

    //凡是继承于UIControl的控件都具有相应事件的能力

    UIButton *button=[UIButtonbuttonWithType:UIButtonTypeSystem];

     //确定控件的(x,y)坐标和宽和高

    button.frame=CGRectMake(10,100, 100, 100);

      //确定按钮上显示的文本和响应的方法

    [ button setTitle:@"按钮" forState:UIControlStateNormal];

     //确定按钮的颜色和确定它的状态比如是初始状态还是结束状态

    [button setTitleColor:[UIColorblackColor] forState:UIControlStateNormal];

    

    //button.backgroundColor=[UIColor lightGrayColor];

    //文本颜色和状态

    [button setTitleColor:[UIColorblackColor] forState:UIControlStateHighlighted];

    [button setTitleColor:[UIColorgreenColor] forState:UIControlStateHighlighted];

     //设置UI的背景

    button.backgroundColor=[UIColorlightGrayColor];

    

    

    //添加事件

    [button addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];

    

     //放进视图

    [self.viewaddSubview:button];


IOS(UI)_UIButton(按钮)和UIImageView(图片视图)

    

    /*--------------------------------------------------------------------*/

    

    

    //UIButton *button2=[UIButton buttonWithType:UIButtonTypeSystem];//系统的颜色

    UIButton *button2=[UIButtonbuttonWithType:UIButtonTypeCustom];//是他与背景相关联

    button2.frame=CGRectMake(130,100, 100, 100);

    

    [button2 setTitle:@"自然状态" forState:UIControlStateNormal];

     //初始状态文本的颜色

    [button2 setTitleColor:[UIColoryellowColor] forState:UIControlStateNormal];

    

    [button2 setTitle:@"选中状态" forState:UIControlStateSelected];

      //触发后显示的文字颜色

    [button2 setTitleColor:[UIColorgreenColor] forState:UIControlStateSelected];

    

    //button2.selected=NO;//选中状态

    button2.backgroundColor=[UIColorlightGrayColor];

    //添加事件

    

    [button2 addTarget:self action:@selector(buttonSeletedAction:) forControlEvents:UIControlEventTouchUpInside];

    

    

    

    [self.viewaddSubview:button2];


IOS(UI)_UIButton(按钮)和UIImageView(图片视图)

    

    

    UIButton *button3=[UIButtonbuttonWithType:UIButtonTypeCustom];

    button3.frame=CGRectMake(250,100, 100, 100);

    [button3 setTitle:@"自然状态" forState:UIControlStateNormal];

    [button3 setTitleColor:[UIColorpurpleColor] forState:UIControlStateNormal];

    [button3 setTitle:@"编辑状态" forState:UIControlStateDisabled];

    [button3 setTitleColor:[UIColorredColor] forState:UIControlStateDisabled];

    

    button3.enabled=NO;

    

    [self.viewaddSubview:button3];


IOS(UI)_UIButton(按钮)和UIImageView(图片视图)

    

    /*--------------------------------------------*/

    UIButton *aButton=[UIButtonbuttonWithType:UIButtonTypeCustom];

    aButton.frame=CGRectMake(10,220, 100, 100);

    

    //自然状态

    [aButton setImage:[UIImageimageNamed:@"Button/Off"] forState:UIControlStateNormal];

    //高亮状态

    [aButton setImage:[UIImageimageNamed:@"Button/On"] forState:UIControlStateHighlighted];

    

    [aButton addTarget:self action:@selector(aButtonAction:) forControlEvents:UIControlEventTouchUpInside];

    

    [self.viewaddSubview:aButton];


IOS(UI)_UIButton(按钮)和UIImageView(图片视图)

    /*-------------------------------------------*/

    

    UIButton *bButton=[UIButtonbuttonWithType:UIButtonTypeCustom];

    bButton.frame=CGRectMake(130,220, 100, 100);

    [bButton setImage:[UIImageimageNamed:@"Button/Off"] forState:UIControlStateNormal];

    //被选中状态

    [bButton setImage:[UIImageimageNamed:@"Button/On"] forState:UIControlStateSelected];

    [bButton  addTarget:self action:@selector(bButtonAction:) forControlEvents:UIControlEventTouchUpInside];

    

    [self.viewaddSubview:bButton];



   IOS(UI)_UIButton(按钮)和UIImageView(图片视图) 

    //应用按照说话

    

    UIButton *cButton=[UIButtonbuttonWithType:UIButtonTypeCustom];

   //Screen_width为宏定义 [UIScreen mainScreen].bounds.size.width

    cButton.frame=CGRectMake(Screen_width/2-40,400, 80, 100);

    [cButton setImage:[UIImageimageNamed:@"Button/speakPhone"] forState:UIControlStateNormal];

    //按下时响应的事件

    [cButton addTarget:self action:@selector(cButtonDown:) forControlEvents:UIControlEventTouchDown];

    

    //松开手时响应的事件

    [cButton addTarget:self action:@selector(cButtonUp:) forControlEvents:UIControlEventTouchUpInside];

    

    [self.viewaddSubview:cButton];

    

    UIImageView *imageViewOne=[[UIImageViewalloc] initWithImage:[UIImageimageNamed:@"Button/speak_two"]];

    

    imageViewOne.frame=CGRectMake(Screen_width/2+40,400-30, 30, 30);

    [self.viewaddSubview:imageViewOne];

    

    

    //Button/speak_one为图片路径

    imageViewTwo=[[UIImageViewalloc]initWithImage:[UIImageimageNamed:@"Button/speak_one"]];

    imageViewTwo.frame=CGRectMake(Screen_width/2+40,400-30,30, 30);

    

    imageViewTwo.animationImages=@[[UIImageimageNamed:@"Button/speak_one"],[UIImageimageNamed:@"Button/speak_two"]];

    

    //动画的时间

    imageViewTwo.animationDuration=0.5;

    //是否显示

    imageViewTwo.hidden=YES;

    

    [self.viewaddSubview:imageViewTwo];


IOS(UI)_UIButton(按钮)和UIImageView(图片视图)

  

}


#pragma mark-------方法实现---------

-(void)buttonAction:(UIButton *)sender

{

    NSLog(@"按钮相应事件");

}


-(void)buttonSeletedAction:(UIButton *)sender

{

    sender.selected=!sender.selected;

    NSLog(@"按钮2响应事件");

}


-(void)aButtonAction:(UIButton *)sender

{

    NSLog(@"aButton响应事件");

}


-(void)bButtonAction:(UIButton *)sender

{

    //取反

    sender.selected=!sender.selected;

    NSLog(@"bButton响应事件");

}


-(void)cButtonDown:(UIButton *)sender

{

    imageViewTwo.hidden=NO;

    //开始动画

    [imageViewTwostartAnimating];

    NSLog(@"cButtonDown");

}


-(void)cButtonUp:(UIButton *)sender

{

    imageViewTwo.hidden=YES;

    //结束动画

    [imageViewTwostopAnimating];

    NSLog(@"cButtonUp");

}



- (void)didReceiveMemoryWarning {

    [super didReceiveMemoryWarning];

    // Dispose of any resources that can be recreated.

}


@end