ios按钮长按事件

//

//  ViewController.m

//  按钮长按事件

//

//  Created by soft-angel on 16/3/30.

//  Copyright © 2016 softangel. All rights reserved.

//


#import "ViewController.h"


@interface ViewController ()

{

    UIButton * _btn1;

    UIButton * _btn2;

    UIButton * _btn3;

}


@end


@implementation ViewController


- (void)viewDidLoad {

    [super viewDidLoad];

    // Do any additional setup after loading the view, typically from a nib.

    

    _btn1 = [[UIButton alloc]initWithFrame:CGRectMake(120, 260, 80, 80)];

    [_btn1 addTarget:self action:@selector(btn1:) forControlEvents:UIControlEventTouchUpInside];

    [_btn1 setBackgroundImage:[UIImage imageNamed:@"2.png"] forState:UIControlStateNormal];

    UILongPressGestureRecognizer * longGesture = [[UILongPressGestureRecognizer alloc]initWithTarget:self action:@selector(longGesture:)];

    longGesture.minimumPressDuration = 0.5f;

    [_btn1 addGestureRecognizer:longGesture];

    

    _btn2 = [[UIButton alloc]initWithFrame:CGRectMake(120, 260, 80, 80)];

    [_btn2 addTarget:self action:@selector(btn2:) forControlEvents:UIControlEventTouchUpInside];

    [_btn2 setBackgroundImage:[UIImage imageNamed:@"1.png"] forState:UIControlStateNormal];

    [self.view addSubview:_btn2];

    

    _btn3 = [[UIButton alloc]initWithFrame:CGRectMake(120, 260, 80, 80)];

    [_btn3 addTarget:self action:@selector(btn3:) forControlEvents:UIControlEventTouchUpInside];

    [_btn3 setBackgroundImage:[UIImage imageNamed:@"3.png"] forState:UIControlStateNormal];

    [self.view addSubview:_btn3];

    

    [self.view addSubview:_btn1];

}


-(void)show

{

    [UIView beginAnimations:nil context:nil];

    [UIView setAnimationDuration:0.5f];

    [UIView setAnimationCurve:7];

    _btn2.frame = CGRectMake(80, 150, 80, 80);

    _btn3.frame = CGRectMake(180, 150, 80, 80);

    [UIView commitAnimations];

}


-(void)hidden

{

    [UIView animateWithDuration:0.5f animations:^{

        _btn2.frame = _btn1.frame;

        _btn3.frame = _btn1.frame;

    } completion:nil];

}



-(void)btn1:(UIButton *)sender

{

    NSLog(@"111");

}


-(void)btn2:(UIButton *)sender

{

    NSLog(@"222");

    [self hidden];

}


-(void)btn3:(UIButton *)sender

{

    NSLog(@"333");

    [self hidden];

}


-(void)longGesture:(UILongPressGestureRecognizer *)gesture

{

    if (gesture.state == UIGestureRecognizerStateBegan)

    {

        [self show];

    }

}


-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event

{

    [self hidden];

}


- (void)didReceiveMemoryWarning {

    [super didReceiveMemoryWarning];

    // Dispose of any resources that can be recreated.

}


@end

你可能感兴趣的:(ios按钮长按事件)