Objective-C语言——UIButton 按钮



#import "ViewController.h"

@interface ViewController ()

{
    UIImageView *imageViewTwo;

}


@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    
    
    //UIButton
    //凡是继承于 UIControl 的控件都具有响应事件的能力
    UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];
    
    button.frame = CGRectMake(10, 100, 100, 100);
    
    [button setTitle:@"我是按钮" forState:UIControlStateNormal];
    
    [button setTitleColor:[UIColor purpleColor] forState:UIControlStateNormal];
    
    button.backgroundColor = [UIColor redColor];
    
    [button setTitle:@"高亮状态" forState:UIControlStateHighlighted];
    
    [button setTitleColor:[UIColor blackColor] forState:UIControlStateHighlighted];
    

    //添加事件
    [button addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
    
    
    [self.view addSubview:button];
    
    
    
    
    
    
    
    UIButton *button2 = [UIButton buttonWithType:UIButtonTypeCustom];
    
    button2.frame = CGRectMake(130, 100, 100, 100);
    
    [button2 setTitle:@"自然状态" forState:UIControlStateNormal];
    
    [button2 setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    
    [button2 setTitle:@"选中状态" forState: UIControlStateSelected];
    
    [button2 setTitleColor:[UIColor blueColor] forState:UIControlStateSelected];
    
    button2.backgroundColor = [UIColor lightGrayColor];
    

    //添加事件
    [button2 addTarget:self action:@selector(buttonAction2:) 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:@"off.jpg"] forState:UIControlStateNormal];
    
    //高亮状态
    [aButton setImage:[UIImage imageNamed:@"on.jpg"] 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:@"off.jpg"] forState:UIControlStateNormal];
    //被选中状态
    [bButton setImage:[UIImage imageNamed:@"on.jpg"] forState:UIControlStateSelected];
    
    [bButton addTarget:self action:@selector(bButtonAction:) forControlEvents:UIControlEventTouchUpInside];
    
    [self.view addSubview:bButton];
    
    
    
    
    
    
    
    
    //应用,按住说话
    UIButton *cButton = [UIButton buttonWithType:UIButtonTypeCustom];
    
    cButton.frame = CGRectMake(ScreenWidth/2-80/2, 400, 80, 100);
    
    [cButton setImage:[UIImage imageNamed:@"speakPhone.jpg"] forState:UIControlStateNormal];
    //按下时 响应的事件
    [cButton addTarget:self action:@selector(cButtonDown:) forControlEvents:UIControlEventTouchDown];
    
    //松开时响应的事件
    [cButton addTarget:self action:@selector(cBunttonUp:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:cButton];
    
    
    UIImageView *imageViewOne = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"one.jpg"]];
    
    imageViewOne.frame = CGRectMake(ScreenWidth/2+40, 400-30, 30, 30);
    
        [self.view addSubview:imageViewOne];
    
    
    imageViewTwo = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"two.jpg"]];
     imageViewTwo.frame = CGRectMake(ScreenWidth/2+40, 400-30, 30, 30);
    
    imageViewTwo.animationImages = @[[UIImage imageNamed:@"one.jpg"],[UIImage imageNamed:@"two.jpg"]];
    
    imageViewTwo.animationDuration = 0.5;
    
    imageViewTwo.hidden = YES;
    
    [self.view addSubview:imageViewTwo];
    

    
    
    
    

    
    
    
    
    
    
    
}


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

-(void)buttonAction:(UIButton *)sender
{

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

}

-(void)buttonAction2:(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)cBunttonUp:(UIButton *)sender
{
    //隐藏
    imageViewTwo.hidden = YES;
    //停止动画
    [imageViewTwo stopAnimating];
    NSLog(@"cBunttonUp");

}


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

@end

Objective-C语言——UIButton 按钮_第1张图片

你可能感兴趣的:(Objective-C语言)