iPhone 中处理手机触摸屏的手势操作在3.2版本以前是UIResponder,它是需要程序分辨不同的手势操作来做出相应的反应!而在3.2以后苹果公司提供了一种渐变快捷的方法:UIGestureResponder的方法;以下介绍以下UIGestureResponder方法的使用;
手势:
如果用户需要定义手势,需要 touchBegain touchMove touchEnd touchCancle 这四个方法,系统重写这四个类方法,为我们提供了用户常见的手势, UIGestureRecognizer 系统所有手势的父类, 在这父类中为所有手势定义了, 初始化方法。
- (instancetype)initWithTarget:(id)target action:(SEL)action;
手势的状态,state
当前手势响应的视图 view
以及几个重要的方法
// 当前手势屏蔽另外的手势
- (void)requireGestureRecognizerToFail:(UIGestureRecognizer *)otherGestureRecognizer;
// 当前手势在view上的位置
- (CGPoint)locationInView:(UIView*)view;
// 当前手势有多少个手指
- (NSUInteger)numberOfTouches;
系统定义了一些常见的手势
UITapGestureRecognizer: 轻拍(单击或者双击)
UIPinchGestureRecognizer: 捏合
UIPanGestureRecognizer:拖动
UILongPressGestureRecognizer: 长按
UISwipeGestureRecognizer: 轻扫
UIRotationGestureRecognizer: 旋转
*/
少废话,直接上代码
首先我们以对一个view视图来操作来实现手势的功能
定义一个视图
targetview=[[UIView alloc]initWithFrame:CGRectMake(100, 200, 200, 200)];
targetview.backgroundColor=[UIColor cyanColor];
[self.window addSubview:targetview];
// 实现单击响应事件
//1.创建手势实例,并且连接tapAction来响应手势
UITapGestureRecognizer *singletapGestureRecognizer=[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapAction:)];
// 给手势添加辨认
[targetview addGestureRecognizer:singletapGestureRecognizer];
// 实现双击响应点击事件 doubleTAP 来响应手势
UITapGestureRecognizer *doubleTap=[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(doubleTAP:)];
设置点击的次数实现双击
doubleTap.numberOfTapsRequired=2;
// 单击入要执行,必须双击失效,实现互斥
[singletapGestureRecognizer requireGestureRecognizerToFail:doubleTap];
//给视图添加手势
[targetview addGestureRecognizer:doubleTap];
添加捏合手势 pinchTap:
UIPinchGestureRecognizer *pinch=[[UIPinchGestureRecognizer alloc]initWithTarget:self action:@selector(pinchTap:)];
[targetview addGestureRecognizer:pinch];
//手势拖动
UIPanGestureRecognizer *pan=[[UIPanGestureRecognizer alloc]initWithTarget:self action:@selector(pan:)];
// 添加手势
[targetview addGestureRecognizer:pan];
//长按添加事件
UILongPresszGestureRecognizer *longP=
[[UILongPressGestureRecognizer alloc]initWithTarget:self action:@selector(longAction:)];
// 给视图添加事件
[targetview addGestureRecognizer:longP];
// 轻扫添加事件
UISwipeGestureRecognizer *swipe=[[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(swipe:)];
swipe.direction=UISwipeGestureRecognizerDirectionLeft;
//给视图添加拖动手势
[targetview addGestureRecognizer:swipe];
//添加选装手势
UIRotationGestureRecognizer *ratation=[[UIRotationGestureRecognizer alloc]initWithTarget:self action:@selector(rotaA:)];
//给视图添加旋转的手势
[targetview addGestureRecognizer:ratation];
self.window.backgroundColor = [UIColor whiteColor];
[self.window makeKeyAndVisible];
return YES;
}
//旋转
-(void)rotaA:(UIRotationGestureRecognizer *)sender
{
sender.view.transform=CGAffineTransformRotate(sender.view.transform, sender.rotation);
}
//轻扫
-(void)swipe:(UISwipeGestureRecognizer *)sender
{
switch (sender.direction) {
case UISwipeGestureRecognizerDirectionLeft:
NSLog(@" 左");
break;
case UISwipeGestureRecognizerDirectionRight:
break;
case UISwipeGestureRecognizerDirectionDown:
break;
case UISwipeGestureRecognizerDirectionUp:
break;
default:
break;
}
}
-(void)tapAction:(UITapGestureRecognizer *)sender
{
NSLog(@"单击");
}
-(void)doubleTAP:(UITapGestureRecognizer *)sender
{
NSLog(@"双击");
}
-(void)pinchTap:(UIPinchGestureRecognizer *)sender
{
sender.view.transform=CGAffineTransformScale(sender.view.transform, sender.scale, sender.scale);
}
-(void)pan:(UIPanGestureRecognizer *)sender
{
CGPoint point=[sender translationInView:self.window];
sender.view.center=CGPointMake(sender.view.center.x+point.x, sender.view.center.y+point.y);
[sender setTranslation:CGPointZero inView:self.window ];
}
-(void)longAction:(UILongPressGestureRecognizer *)sender
{
// 根据手势的状态
if (sender.state==UIGestureRecognizerStateEnded) {
//
UIAlertView *alert=[[UIAlertView alloc]initWithTitle:@"长按" message:@"长按" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"button1",@"button2", nil];
//
[alert dismissWithClickedButtonIndex:0 animated:YES];
// UITextField *text=[alert textFieldAtIndex:3];
// [alert show];
// UIAlertView *alertview=[[UIAlertView alloc]initWithTitle:@"登陆" message:nil delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"登陆", nil];
// [alertview setAlertViewStyle:UIAlertViewStyleLoginAndPasswordInput];
// UITextField *text=[alertview textFieldAtIndex:0];
// UITextField *text1=[alertview textFieldAtIndex:1];
// [alertview show];
UIActionSheet *actionSheet=[[UIActionSheet alloc]initWithTitle:@"title" delegate:self cancelButtonTitle:@"取消" destructiveButtonTitle:@"1" otherButtonTitles:@"2",@"3", nil];
[actionSheet showInView:self.window];
}
}
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
NSLog(@"buttonindex=%ld",buttonIndex);
switch (buttonIndex) {
case 0:
NSLog(@"button");
break;
case 1:
NSLog(@"button2");
break;
case 2:
NSLog(@"取消");
break;
case 3:
NSLog(@"hhh");
break;
default:
break;
}
}
//actionsheet
-(void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
{
switch (buttonIndex) {
case 0:
NSLog(@"button");
break;
case 1:
NSLog(@"button2");
break;
case 2:
NSLog(@"取消");
break;
case 3:
NSLog(@"hhh");
break;
default:
break;
}
}
-(void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event{
//手势取消!
}