ios按钮长按事件

来源:互联网 发布:明星淘宝店哪里找 编辑:程序博客网 时间:2024/05/16 12:01

//

//  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 {

    [superviewDidLoad];

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

    

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

    [_btn1addTarget:selfaction:@selector(btn1:)forControlEvents:UIControlEventTouchUpInside];

    [_btn1setBackgroundImage:[UIImageimageNamed:@"2.png"]forState:UIControlStateNormal];

    UILongPressGestureRecognizer * longGesture = [[UILongPressGestureRecognizeralloc]initWithTarget:selfaction:@selector(longGesture:)];

    longGesture.minimumPressDuration = 0.5f;

    [_btn1addGestureRecognizer:longGesture];

    

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

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

    [_btn2setBackgroundImage:[UIImageimageNamed:@"1.png"] forState:UIControlStateNormal];

    [self.viewaddSubview:_btn2];

    

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

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

    [_btn3setBackgroundImage:[UIImageimageNamed:@"3.png"] forState:UIControlStateNormal];

    [self.viewaddSubview:_btn3];

    

    [self.view addSubview:_btn1];

}


-(void)show

{

    [UIViewbeginAnimations:nil context:nil];

    [UIViewsetAnimationDuration:0.5f];

    [UIViewsetAnimationCurve:7];

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

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

    [UIViewcommitAnimations];

}


-(void)hidden

{

    [UIViewanimateWithDuration: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");

    [selfhidden];

}


-(void)btn3:(UIButton *)sender

{

    NSLog(@"333");

    [selfhidden];

}


-(void)longGesture:(UILongPressGestureRecognizer *)gesture

{

    if (gesture.state ==UIGestureRecognizerStateBegan)

    {

        [selfshow];

    }

}


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

{

    [selfhidden];

}


- (void)didReceiveMemoryWarning {

    [super didReceiveMemoryWarning];

    // Dispose of any resources that can be recreated.

}


@end

0 0
原创粉丝点击