关于点击 button 让一个视图出现,松开后消失!

//直接上代码
- (void)viewDidLoad {
   
    [super viewDidLoad];
    
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    
    button.frame = CGRectMake(50, 150, 50, 50);
    
    button.backgroundColor = [UIColor redColor];
    
    [button addTarget:self action:@selector(buttonClick0:) forControlEvents:UIControlEventTouchDown];
    
    [button addTarget:self action:@selector(buttonClick1:) forControlEvents:UIControlEventTouchDownRepeat];
    
    [button addTarget:self action:@selector(buttonClick2:) forControlEvents:UIControlEventTouchDragInside];
    
    [button addTarget:self action:@selector(buttonClick3:) forControlEvents:UIControlEventTouchDragOutside];
    
    [button addTarget:self action:@selector(buttonClick4:) forControlEvents:UIControlEventTouchDragEnter];
    
    [button addTarget:self action:@selector(buttonClick5:) forControlEvents:UIControlEventTouchDragExit];
    
    [button addTarget:self action:@selector(buttonClick6:) forControlEvents:UIControlEventTouchUpInside];
    
    [button addTarget:self action:@selector(buttonClick7:) forControlEvents:UIControlEventTouchDragOutside];
    
    [button addTarget:self action:@selector(buttonClick8:) forControlEvents:UIControlEventTouchCancel];
    
    [self.view addSubview:button];
    
    showView = [[UIView alloc]initWithFrame:CGRectMake(100, 200, 50, 50)];
    showView.backgroundColor = [UIColor blueColor];
}

- (void)buttonClick0:(UIButton *)sender {
    
    NSLog(@"000000");
    [self.view addSubview:showView];
}
- (void)buttonClick1:(UIButton *)sender {
    
    NSLog(@"111111");
}
- (void)buttonClick2:(UIButton *)sender {
    
    NSLog(@"22222");
}
- (void)buttonClick3:(UIButton *)sender {
    
    NSLog(@"333333");
}
- (void)buttonClick4:(UIButton *)sender {
    
    NSLog(@"4444");
}
- (void)buttonClick5:(UIButton *)sender {
    
    NSLog(@"55555");
}
- (void)buttonClick6:(UIButton *)sender {
    
    [showView removeFromSuperview];
    NSLog(@"6666");
}
- (void)buttonClick7:(UIButton *)sender {
    
    NSLog(@"77777");
}
- (void)buttonClick8:(UIButton *)sender {
    
    NSLog(@"8888");
}
- (void)buttonClick9:(UIButton *)sender {
    
    NSLog(@"999999");
}

//通过观看控制台的输出情况发现只有000000和66666是有效的,所以我们只要在按下的时候调用00000也就是UIControlEventTouchDown的方法,松开手后是66666也就是UIControlEventTouchUpInside的方法.

推荐阅读更多精彩内容