几种手势操作的最基本实现
2014-05-04 17:45
316 查看
工作太忙,有点懒,直接上代码了。
#import "ViewController.h"
@interface
ViewController ()
@end
@implementation ViewController
/*
UITapGestureRecognizer Tap(点击)
UIPinchGestureRecognizer Pinch(捏合)
UIRotationGestureRecognizer Rotation(旋转)
UISwipeGestureRecognizer Swipe(滑动,快速移动,是用于监测滑动的方向的)
UIPanGestureRecognizer Pan (拖移,慢速移动,是用于监测偏移的量的)
UILongPressGestureRecognizer LongPress(长按)
*/
- (void)viewDidLoad
{
[super
viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
UIButton *btn = [UIButton
buttonWithType:UIButtonTypeRoundedRect];
[btn
setFrame:CGRectMake(50,
50, 100,
100)];
[btn setBackgroundImage:[UIImage
imageNamed:@"1.png"]
forState:UIControlStateNormal];
//
拖移的 Recognizer
UIPanGestureRecognizer *panGestureRecognizer = [[UIPanGestureRecognizer
alloc]initWithTarget:self
action:@selector(handlePan:)];
[btn
addGestureRecognizer:panGestureRecognizer];
UIView *tapView = [[UIView
alloc] initWithFrame:CGRectMake(10,
50, 300,
300)];
[tapView setBackgroundColor:[UIColor
redColor]];
//
单击的 Recognizer
UITapGestureRecognizer* singleRecognizer;
singleRecognizer = [[UITapGestureRecognizer
alloc] initWithTarget:self
action:@selector(SingleTap:)];
//点击的次数
singleRecognizer.numberOfTapsRequired =
1; // 单击
//点击的手指数
singleRecognizer.numberOfTouchesRequired =
2;
//给view添加一个手势监测;
[tapView
addGestureRecognizer:singleRecognizer];
//
双击的 Recognizer
UITapGestureRecognizer* doubleRecognizer;
doubleRecognizer = [[UITapGestureRecognizer
alloc] initWithTarget:self
action:@selector(DoubleTap:)];
doubleRecognizer.numberOfTapsRequired =
2; // 双击
//关键语句,给self.view添加一个手势监测;
[tapView
addGestureRecognizer:doubleRecognizer];
//
关键在这一行,双击手势确定监测失败才会触发单击手势的相应操作
[singleRecognizer
requireGestureRecognizerToFail:doubleRecognizer];
//
捏合的 Recognizer
UIPinchGestureRecognizer *pinchGestureRecognizer = [[UIPinchGestureRecognizer
alloc] initWithTarget:self
action:@selector(handlePinch:)];
[tapView
addGestureRecognizer:pinchGestureRecognizer];
//
旋转的 Recognizer
UIRotationGestureRecognizer *rotateRecognizer = [[UIRotationGestureRecognizer
alloc]
initWithTarget:self
action:@selector(handleRotate:)];
[tapView
addGestureRecognizer:rotateRecognizer];
//
长按的 Recognizer
UILongPressGestureRecognizer *longPressRecognizer = [[UILongPressGestureRecognizer
alloc] initWithTarget:self
action:@selector(handleLongPress:)];
//设置长按时间间隔
[longPressRecognizer
setMinimumPressDuration:1.0];
[tapView
addGestureRecognizer:longPressRecognizer];
//
滑动的 Recognizer
UISwipeGestureRecognizer *swipeRecognizer = [[UISwipeGestureRecognizer
alloc] initWithTarget:self
action:@selector(handleSwipe:)];
//设置滑动方向
[swipeRecognizer setDirection:UISwipeGestureRecognizerDirectionDown];
[tapView
addGestureRecognizer:swipeRecognizer];
[self.view
addSubview:tapView];
[self.view
addSubview:btn];
}
- (void)handlePan:(UIPanGestureRecognizer*) recognizer
{
NSLog(@"拖移,慢速移动");
CGPoint translation = [recognizer
translationInView:self.view];
recognizer.view.center =
CGPointMake(recognizer.view.center.x + translation.x, recognizer.view.center.y
+ translation.y);
[recognizer setTranslation:CGPointZero
inView:self.view];
}
-(void)SingleTap:(UITapGestureRecognizer*)recognizer
{
//处理单击操作
NSLog(@"单击");
}
-(void)DoubleTap:(UITapGestureRecognizer*)recognizer
{
//处理双击操作
NSLog(@"双击");
}
- (void) handlePinch:(UIPinchGestureRecognizer*) recognizer
{
NSLog(@"捏合, %f", recognizer.scale);
recognizer.view.transform =
CGAffineTransformScale(recognizer.view.transform, recognizer.scale, recognizer.scale);
recognizer.scale =
1;
}
- (void) handleRotate:(UIRotationGestureRecognizer*) recognizer
{
NSLog(@"旋转, %f", recognizer.rotation);
recognizer.view.transform =
CGAffineTransformRotate(recognizer.view.transform, recognizer.rotation);
recognizer.rotation =
0;
}
-(void)handleLongPress:(UILongPressGestureRecognizer*)recognizer
{
//处理长按操作
NSLog(@"长按, %f",
recognizer.minimumPressDuration);
}
-(void)handleSwipe:(UISwipeGestureRecognizer*)recognizer
{
//处理滑动操作
NSLog(@"滑动,快速移动");
CGPoint translation = [recognizer
locationInView:self.view];
NSLog(@"Swipe - start location: %f,%f", translation.x, translation.y);
// recognizer.view.transform = CGAffineTransformMakeTranslation(translation.x, translation.y);
}
#import "ViewController.h"
@interface
ViewController ()
@end
@implementation ViewController
/*
UITapGestureRecognizer Tap(点击)
UIPinchGestureRecognizer Pinch(捏合)
UIRotationGestureRecognizer Rotation(旋转)
UISwipeGestureRecognizer Swipe(滑动,快速移动,是用于监测滑动的方向的)
UIPanGestureRecognizer Pan (拖移,慢速移动,是用于监测偏移的量的)
UILongPressGestureRecognizer LongPress(长按)
*/
- (void)viewDidLoad
{
[super
viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
UIButton *btn = [UIButton
buttonWithType:UIButtonTypeRoundedRect];
[btn
setFrame:CGRectMake(50,
50, 100,
100)];
[btn setBackgroundImage:[UIImage
imageNamed:@"1.png"]
forState:UIControlStateNormal];
//
拖移的 Recognizer
UIPanGestureRecognizer *panGestureRecognizer = [[UIPanGestureRecognizer
alloc]initWithTarget:self
action:@selector(handlePan:)];
[btn
addGestureRecognizer:panGestureRecognizer];
UIView *tapView = [[UIView
alloc] initWithFrame:CGRectMake(10,
50, 300,
300)];
[tapView setBackgroundColor:[UIColor
redColor]];
//
单击的 Recognizer
UITapGestureRecognizer* singleRecognizer;
singleRecognizer = [[UITapGestureRecognizer
alloc] initWithTarget:self
action:@selector(SingleTap:)];
//点击的次数
singleRecognizer.numberOfTapsRequired =
1; // 单击
//点击的手指数
singleRecognizer.numberOfTouchesRequired =
2;
//给view添加一个手势监测;
[tapView
addGestureRecognizer:singleRecognizer];
//
双击的 Recognizer
UITapGestureRecognizer* doubleRecognizer;
doubleRecognizer = [[UITapGestureRecognizer
alloc] initWithTarget:self
action:@selector(DoubleTap:)];
doubleRecognizer.numberOfTapsRequired =
2; // 双击
//关键语句,给self.view添加一个手势监测;
[tapView
addGestureRecognizer:doubleRecognizer];
//
关键在这一行,双击手势确定监测失败才会触发单击手势的相应操作
[singleRecognizer
requireGestureRecognizerToFail:doubleRecognizer];
//
捏合的 Recognizer
UIPinchGestureRecognizer *pinchGestureRecognizer = [[UIPinchGestureRecognizer
alloc] initWithTarget:self
action:@selector(handlePinch:)];
[tapView
addGestureRecognizer:pinchGestureRecognizer];
//
旋转的 Recognizer
UIRotationGestureRecognizer *rotateRecognizer = [[UIRotationGestureRecognizer
alloc]
initWithTarget:self
action:@selector(handleRotate:)];
[tapView
addGestureRecognizer:rotateRecognizer];
//
长按的 Recognizer
UILongPressGestureRecognizer *longPressRecognizer = [[UILongPressGestureRecognizer
alloc] initWithTarget:self
action:@selector(handleLongPress:)];
//设置长按时间间隔
[longPressRecognizer
setMinimumPressDuration:1.0];
[tapView
addGestureRecognizer:longPressRecognizer];
//
滑动的 Recognizer
UISwipeGestureRecognizer *swipeRecognizer = [[UISwipeGestureRecognizer
alloc] initWithTarget:self
action:@selector(handleSwipe:)];
//设置滑动方向
[swipeRecognizer setDirection:UISwipeGestureRecognizerDirectionDown];
[tapView
addGestureRecognizer:swipeRecognizer];
[self.view
addSubview:tapView];
[self.view
addSubview:btn];
}
- (void)handlePan:(UIPanGestureRecognizer*) recognizer
{
NSLog(@"拖移,慢速移动");
CGPoint translation = [recognizer
translationInView:self.view];
recognizer.view.center =
CGPointMake(recognizer.view.center.x + translation.x, recognizer.view.center.y
+ translation.y);
[recognizer setTranslation:CGPointZero
inView:self.view];
}
-(void)SingleTap:(UITapGestureRecognizer*)recognizer
{
//处理单击操作
NSLog(@"单击");
}
-(void)DoubleTap:(UITapGestureRecognizer*)recognizer
{
//处理双击操作
NSLog(@"双击");
}
- (void) handlePinch:(UIPinchGestureRecognizer*) recognizer
{
NSLog(@"捏合, %f", recognizer.scale);
recognizer.view.transform =
CGAffineTransformScale(recognizer.view.transform, recognizer.scale, recognizer.scale);
recognizer.scale =
1;
}
- (void) handleRotate:(UIRotationGestureRecognizer*) recognizer
{
NSLog(@"旋转, %f", recognizer.rotation);
recognizer.view.transform =
CGAffineTransformRotate(recognizer.view.transform, recognizer.rotation);
recognizer.rotation =
0;
}
-(void)handleLongPress:(UILongPressGestureRecognizer*)recognizer
{
//处理长按操作
NSLog(@"长按, %f",
recognizer.minimumPressDuration);
}
-(void)handleSwipe:(UISwipeGestureRecognizer*)recognizer
{
//处理滑动操作
NSLog(@"滑动,快速移动");
CGPoint translation = [recognizer
locationInView:self.view];
NSLog(@"Swipe - start location: %f,%f", translation.x, translation.y);
// recognizer.view.transform = CGAffineTransformMakeTranslation(translation.x, translation.y);
}
相关文章推荐
- 【C++数据结构】几种单链表的模类板实现及基本操作
- JDBC实现数据库的几种基本操作(查询,分页查询,根据关键字进行查询以及插入数据)
- 栈的链式存储 c实现的几种基本操作
- 线性表链式存储结构下基本操作的实现(初始化、赋值、取值、插入、删除、归并等)
- 数组实现的线性表的基本操作2[C语言]
- [寒江孤叶丶的Cocos2d-x之旅_36]用LUA实现UTF8的字符串基本操作 UTF8字符串长度,UTF8字符串剪裁等
- 二叉树的基本操作以及相关问题的求解—C++实现
- 重学数据结构003——栈的基本操作及实现(链式存储)
- Java的IO操作(二) - 带缓冲区的流对象、写入基本数据类型、实现命令行中的copy命令
- C语言实现单链队列的基本操作(五)
- c语言实现循环链表的基本操作
- 二叉搜索树的基本操作实现
- c语言实现双向链表的基本操作
- c语言顺序表的实现与基本操作
- 利用C#2005实现数据表的基本操作
- 实验一线性表的基本操作实现及其应用
- 单链表 基本操作 的实现源码
- 集合框架_用户登录注册案例详细分析和分包的实现、用户基本描述类和用户操作接口的实现、用户操作类的具体实现、用户测试类的实现、加入猜数字小游戏
- 几种基本的排序算法(选择排序,冒泡排序,快速排序,归并排序,希尔排序)C语言实现