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

#import "ViewController.h"
@interface ViewController ()
{
    UIImageView *imageViewTwo;
}


@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    /*
    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=[UIButton buttonWithType:UIButtonTypeSystem];
    
    button.frame=CGRectMake(10, 100, 100, 100);
    [ button setTitle:@"按钮" forState:UIControlStateNormal];
    
    [button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    
    //button.backgroundColor=[UIColor lightGrayColor];
    [button setTitleColor:[UIColor blackColor] forState:UIControlStateHighlighted];
    [button setTitleColor:[UIColor greenColor] forState:UIControlStateHighlighted];
    
    button.backgroundColor=[UIColor lightGrayColor];
    
    
    //添加事件
    [button addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
    
    
    [self.view addSubview:button];
    
    /*--------------------------------------------------------------------*/
    
    
    //UIButton *button2=[UIButton buttonWithType:UIButtonTypeSystem];//系统的颜色
    UIButton *button2=[UIButton buttonWithType:UIButtonTypeCustom];//是他与背景相关联
    button2.frame=CGRectMake(130, 100, 100, 100);
    
    [button2 setTitle:@"自然状态" forState:UIControlStateNormal];
    [button2 setTitleColor:[UIColor yellowColor] forState:UIControlStateNormal];
    
    [button2 setTitle:@"选中状态" forState:UIControlStateSelected];
    [button2 setTitleColor:[UIColor greenColor] forState:UIControlStateSelected];
    
    //button2.selected=NO;//选中状态
    button2.backgroundColor=[UIColor lightGrayColor];
    //添加事件
    
    [button2 addTarget:self action:@selector(buttonSeletedAction:) forControlEvents:UIControlEventTouchUpInside];
    
    
    
    [self.view addSubview:button2];
    
    
    UIButton *button3=[UIButton buttonWithType:UIButtonTypeCustom];
    button3.frame=CGRectMake(250, 100, 100, 100);
    [button3 setTitle:@"自然状态" forState:UIControlStateNormal];
    [button3 setTitleColor:[UIColor purpleColor] forState:UIControlStateNormal];
    [button3 setTitle:@"编辑状态" forState:UIControlStateDisabled];
    [button3 setTitleColor:[UIColor redColor] forState:UIControlStateDisabled];
    
    button3.enabled=NO;
    
    [self.view addSubview:button3];
    
    /*--------------------------------------------*/
    UIButton *aButton=[UIButton buttonWithType:UIButtonTypeCustom];
    aButton.frame=CGRectMake(10, 220, 100, 100);
    
    //自然状态
    [aButton setImage:[UIImage imageNamed:@"Button/Off"] forState:UIControlStateNormal];
    //高亮状态
    [aButton setImage:[UIImage imageNamed:@"Button/On"] forState:UIControlStateHighlighted];
    
    [aButton addTarget:self action:@selector(aButtonAction:) forControlEvents:UIControlEventTouchUpInside];
    
    [self.view addSubview:aButton];
    /*-------------------------------------------*/
    UIButton *bButton=[UIButton buttonWithType:UIButtonTypeCustom];
    bButton.frame=CGRectMake(130, 220, 100, 100);
    [bButton setImage:[UIImage imageNamed:@"Button/Off"] forState:UIControlStateNormal];
    //被选中状态
    [bButton setImage:[UIImage imageNamed:@"Button/On"] forState:UIControlStateSelected];
    [bButton  addTarget:self action:@selector(bButtonAction:) forControlEvents:UIControlEventTouchUpInside];
    
    [self.view addSubview:bButton];
    
    //应用按照说话
    
    UIButton *cButton=[UIButton buttonWithType:UIButtonTypeCustom];
    cButton.frame=CGRectMake(Screen_width/2-40, 400, 80, 100);
    [cButton setImage:[UIImage imageNamed:@"Button/speakPhone"] forState:UIControlStateNormal];
    //按下时响应的事件
    [cButton addTarget:self action:@selector(cButtonDown:) forControlEvents:UIControlEventTouchDown];
    
    //松开手时响应的事件
    [cButton addTarget:self action:@selector(cButtonUp:) forControlEvents:UIControlEventTouchUpInside];
    
    [self.view addSubview:cButton];
    
    UIImageView *imageViewOne=[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"Button/speak_two"]];
    
    imageViewOne.frame=CGRectMake(Screen_width/2+40, 400-30, 30, 30);
    [self.view addSubview:imageViewOne];
    
    imageViewTwo=[[UIImageView alloc]initWithImage:[UIImage imageNamed:@"Button/speak_one"]];
    imageViewTwo.frame=CGRectMake(Screen_width/2+40, 400-30, 30, 30);
    
    imageViewTwo.animationImages=@[[UIImage imageNamed:@"Button/speak_one"],[UIImage imageNamed:@"Button/speak_two"]];
    
    imageViewTwo.animationDuration=0.5;
    imageViewTwo.hidden=YES;
    
    [self.view addSubview:imageViewTwo];
  
}

#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;
    [imageViewTwo startAnimating];
    NSLog(@"cButtonDown");
}

-(void)cButtonUp:(UIButton *)sender
{
    imageViewTwo.hidden=YES;
    [imageViewTwo stopAnimating];
    NSLog(@"cButtonUp");
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值