ios按钮长按事件
2016-04-21 11:16
344 查看
//
// ViewController.m
// 按钮长按事件
//
// Created by soft-angel on 16/3/30.
// Copyright © 2016年 soft-angel. 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
// ViewController.m
// 按钮长按事件
//
// Created by soft-angel on 16/3/30.
// Copyright © 2016年 soft-angel. 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
相关文章推荐
- AFNetWorking的get与post用法以及进行图片上传
- 如何控制百度地图的显示范围 ios软件开发
- ios按钮滑动切换
- 开发笔记-27个iOS开发中的小技巧
- iOS调整系统屏幕亮度
- iOS开发经验总结
- IOS9获取HTML数据报错解决
- [BS-02] iOS数组、字典、NSNumber 新写法—— @[]、@{}
- iOS设置NavigationBar颜色和BackButtonItem颜色以及navigation的应用
- IOS7 iBeacons探寻
- 多线程 - dispatch_barrier_async 使用及解析
- iOS无法解析报错 std
- [IOS/翻译]Cocoa Touch Layer
- xCode中常见的错误分析
- [IOS/翻译]Media Layer
- iOS语音播放之切换听筒和扬声器的方法解决方案
- [IOS/翻译]Core Services Layer
- IOS开发--上传图片
- iOS小Tip之查看FPS
- ios 3D Touch功能的实现