ios多手势事件
开发ios应用时我们经常用到多手势来处理事情,如给scrollView增加点击事件,scrollView不能响应view的touch事件,但有时候却要用到多手势事件,那么我们可以给这个scrollView增加自己的手指事件。给imageView增加点击、放大、旋转等都可以通过多手势来完成。
下面就介绍下各种的手势的使用方法,及注意事项。
- (void)viewDidLoad {
[super viewDidLoad];
//为了能响应多手势事件,imageView的userInteractionEnabled属性要设为YES.
self.imageView.userInteractionEnabled=YES; //1、手指点击事件
//单指单击
UITapGestureRecognizer *singleFingerOne = [[UITapGestureRecognizer alloc] initWithTarget:self
action:@selector(fingerIncident:)];
//手指数
singleFingerOne.numberOfTouchesRequired = ;
//点击次数
singleFingerOne.numberOfTapsRequired = ;
//设置代理方法
singleFingerOne.delegate= self;
//增加事件者响应者,
[self.imageView addGestureRecognizer:singleFingerOne]; //单指双击
UITapGestureRecognizer *singleFingerTwo = [[UITapGestureRecognizer alloc] initWithTarget:self
action:@selector(fingerIncident:)];
singleFingerTwo.numberOfTouchesRequired = ;
singleFingerTwo.numberOfTapsRequired = ;
singleFingerTwo.delegate= self;
[self.imageView addGestureRecognizer:singleFingerTwo]; //双指单击
UITapGestureRecognizer *doubleFingerOne = [[UITapGestureRecognizer alloc] initWithTarget:self
action:@selector(fingerIncident:)];
doubleFingerOne.numberOfTouchesRequired = ;
doubleFingerOne.numberOfTapsRequired = ;
doubleFingerOne.delegate= self;
[self.imageView addGestureRecognizer:doubleFingerOne]; //双指双击
UITapGestureRecognizer *doubleFingerTwo = [[UITapGestureRecognizer alloc] initWithTarget:self
action:@selector(fingerIncident:)];
doubleFingerTwo.numberOfTouchesRequired = ;
doubleFingerTwo.numberOfTapsRequired = ;
doubleFingerTwo.delegate= self;
[self.imageView addGestureRecognizer:doubleFingerTwo]; //如果不加下面的话,当单指双击时,会先调用单指单击中的处理,再调用单指双击中的处理
[singleFingerOne requireGestureRecognizerToFail:singleFingerTwo];
//同理双指也是如此
[doubleFingerOne requireGestureRecognizerToFail:doubleFingerTwo]; //2、手势为捏的姿势:按住option按钮配合鼠标来做这个动作在虚拟器上
UIPinchGestureRecognizer *pinchGesture=[[UIPinchGestureRecognizer alloc]initWithTarget:self action:@selector(handlePinchGesture:)];
[self.imageView addGestureRecognizer:pinchGesture];//imageView添加手势识别
[pinchGesture release]; //3、旋转手势:按住option按钮配合鼠标来做这个动作在虚拟器上
UIRotationGestureRecognizer *rotateGesture=[[UIRotationGestureRecognizer alloc]initWithTarget:self action:@selector(handleRotateGesture:)];
[self.imageView addGestureRecognizer:rotateGesture];
[rotateGesture release]; //4、拖手势
UIPanGestureRecognizer *panGesture=[[UIPanGestureRecognizer alloc]initWithTarget:self action:@selector(handlePanGesture:)];
[self.imageView addGestureRecognizer:panGesture];
[panGesture release]; //当实现上面拖手势(UIPanGestureRecognizer)后将不能响应下面的左右划(UISwipeGestureRecognizer)事件,事件会被拖手势栏截,所以左右划和拖手势只能选其一
//右划
UISwipeGestureRecognizer *swipeGesture=[[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(handleSwipeGesture:)];
[self.imageView addGestureRecognizer:swipeGesture];
[swipeGesture release];
//左划
UISwipeGestureRecognizer *swipeLeftGesture=[[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(handleSwipeGesture:)];
swipeGesture.direction=UISwipeGestureRecognizerDirectionLeft;//不设置黑夜是右
[self.imageView addGestureRecognizer:swipeLeftGesture];
[swipeLeftGesture release]; UILongPressGestureRecognizer *longpressGesutre=[[UILongPressGestureRecognizer alloc]initWithTarget:self
action:@selector(handleLongpressGesture:)];
//长按时间为1秒
longpressGesutre.minimumPressDuration=0.5;
//允许15秒中运动
longpressGesutre.allowableMovement=;
//所需触摸1次
longpressGesutre.numberOfTouchesRequired=;
[self.imageView addGestureRecognizer:longpressGesutre];
[longpressGesutre release];
} //手指点击事件
- (void)fingerIncident:(UITapGestureRecognizer *)sender
{
if (sender.numberOfTouchesRequired==) {
//单指点击事件
if(sender.numberOfTapsRequired == ) {
//单指单击
NSLog(@"单指单击");
[sender.view.layer removeAllAnimations];
}
else if(sender.numberOfTapsRequired == ){
//单指双击
NSLog(@"单指双击");
}
}
else if (sender.numberOfTouchesRequired==) {
//双指点击事件
if(sender.numberOfTapsRequired == ) {
//双指单击
NSLog(@"双指单击");
}
else if(sender.numberOfTapsRequired == ){
//双指双击
NSLog(@"双指双击");
}
}
} //手指左右滑动事件
-(void)handleSwipeGesture:(UISwipeGestureRecognizer *)sender{
if (sender.direction==UISwipeGestureRecognizerDirectionLeft) {
NSLog(@"左滑");
}
else{
NSLog(@"右滑");
}
} //手指捏事件
-(void)handlePinchGesture:(UIPinchGestureRecognizer *)sender{
NSLog(@"捏"); if([sender state] == UIGestureRecognizerStateEnded) {
lastScale = 1.0;
return;
} CGFloat scale = 1.0 - (lastScale - [(UIPinchGestureRecognizer*)sender scale]); CGAffineTransform newTransform = CGAffineTransformScale(sender.view.transform, scale, scale); [sender.view setTransform:newTransform];
lastScale = [sender scale];
} //手指旋转事件
-(void)handleRotateGesture:(UIRotationGestureRecognizer *)sender{
NSLog(@"旋转"); NSLog(@"sender.scale=%f",sender.rotation);
NSLog(@"sender.velocity=%f",sender.velocity); sender.view.transform=CGAffineTransformRotate(sender.view.transform, sender.rotation);
sender.rotation=;
} //手指拖事件
-(void)handlePanGesture:(UIPanGestureRecognizer *)sender{
NSLog(@"拖");
NSLog(@"sender=%@",sender);
CGPoint translation=[(UIPanGestureRecognizer*)sender translationInView:self.view]; //开始
if (sender.state==UIGestureRecognizerStateBegan) {
x=sender.view.center.x;
y=sender.view.center.y;
}
//进行中
if (sender.state==UIGestureRecognizerStateChanged) {
sender.view.center=CGPointMake(x+translation.x, y+translation.y);
}
//结束
if (sender.state==UIGestureRecognizerStateEnded) {
sender.view.center=CGPointMake(x+translation.x, y+translation.y);
}
} //手指长按事件
-(void)handleLongpressGesture:(UILongPressGestureRecognizer *)sender{
NSLog(@"长按");
if (sender.state==UIGestureRecognizerStateBegan) {
CABasicAnimation* rotationAnimation_ = [CABasicAnimation animationWithKeyPath:@"transform.rotation.z"];
//角度转弧度
rotationAnimation_.fromValue = [NSNumber numberWithFloat:(-0.02)];
rotationAnimation_.toValue = [NSNumber numberWithFloat:(0.02)];
//动画时间
rotationAnimation_.duration = 0.1f;
//动画次数
rotationAnimation_.repeatCount=FLT_MAX;
//回退动画
rotationAnimation_.autoreverses = YES;
//动画开始结束的快慢,设置为加速
rotationAnimation_.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
[[sender.view layer] addAnimation:rotationAnimation_ forKey:@"revItUpAnimation"];
}
}
ios多手势事件的更多相关文章
- 点击事件touches与ios的手势UIGestureRecognizer
.h文件 @property (weak,nonatomic) IBOutlet UILabel *messageLabel;@property (weak,nonatomic) IBOutlet U ...
- IOS 手势事件的冲突
关于手操作需要强调几点: UIImageView默认是不支持交互的,也就是userInteractionEnabled=NO ,因此要接收触摸事件(手势识别),必须设置userInteractionE ...
- IOS开发中长按的手势事件编程
长按手势事件: 长按按钮1S后改变按钮颜色: // 长按事件 #import "ViewController.h" @interface ViewController (){ UI ...
- ios 的touch事件分析
IOS之触摸事件和手势 13.1 事件概述 13.2 触摸事件 13.3 手势 13.1 事件概述 事件是当用户手指触击屏幕及在屏幕上移动时,系统不断发送给应用程序的对象. 系统将事件按照特定的路 ...
- surface上的手势事件
surface上的手势事件 1.surface上的触控事件测试. 手指触控在CHROME和FIREFOX下会触发touch事件,而IE10是不支持touch事件的. 手指触控在三个浏览器下均会触发全部 ...
- [BS-25] IOS中手势UIGestureRecognizer概述
IOS中手势UIGestureRecognizer概述 一.概述 iPhone中处理触摸屏的操作,在3.2之前是主要使用的是由UIResponder而来的如下4种方式: - (void)touches ...
- windows phone和android,ios的touch事件兼容
1.开发背景 最近用html5写了个小游戏,中间踩过无数坑,有很多甚至百度都百度不到答案,可见html5还真是不成熟,兼容性的复杂度比ie6有过之而无不及,性能那个渣简直无力吐槽.. 好了,吐槽结束, ...
- iOS中—触摸事件详解及使用
iOS中--触摸事件详解及使用 (一)初识 要想学好触摸事件,这第一部分的基础理论是必须要学会的,希望大家可以耐心看完. 1.基本概念: 触摸事件 是iOS事件中的一种事件类型,在iOS中按照事件划分 ...
- JavaScript触摸与手势事件
JavaScript触摸与手势事件 发表于 2012-12-10 由 admin iOS版Safari为了向开发人员传达一些特殊信息,新增了一些专有事件.因为iOS设备既没有鼠标也没有键盘,所以在为移 ...
随机推荐
- Sqli-labs less 36
Less-36 我们直接看到36关的源代码 上面的check_quotes()函数是利用了mysql_real_escape_string()函数进行的过滤. mysql_real_escape_st ...
- ZOJ3560 Re:the Princess(高斯消元法)
题目要读很久才能理解它的意思和笑点(如果你也看过那个笑话的话),读懂之后就会发现是一个高斯消元法的题目,对于我来说难点不在高斯消元,而在于字符串处理.先来说说题意吧: 总共有n个人,n个人都会有一段话 ...
- HDU 1316 How Many Fibs?(java,简单题,大数)
题目 /** * compareTo:根据该数值是小于.等于.或大于 val 返回 -1.0 或 1: public int compareTo(BigInteger val) 将此 BigInteg ...
- POJ 2480 Longge's problem (积性函数,欧拉函数)
题意:求∑gcd(i,n),1<=i<=n思路:f(n)=∑gcd(i,n),1<=i<=n可以知道,其实f(n)=sum(p*φ(n/p)),其中p是n的因子.为什么呢?原因 ...
- Changing the Overridden Method’s Characteristics
修改重写方法的特征 在大多数情况下,我们重写(override)一个 virtual 方法是为了改变它的实现.然后,有时我们却想改变该 virtual 方法的其他的特征,这往往会带来一系列问题. 1) ...
- Good Bye 2015 C. New Year and Domino 二维前缀
C. New Year and Domino They say "years are like dominoes, tumbling one after the other". ...
- gitHub入门指导
Github可以托管各种git库,并提供一个web界面,但与其它像 SourceForge或Google Code这样的服务不同,GitHub的独特卖点在于从另外一个项目进行分支的简易性.为一个项目贡 ...
- 简易解说拉格朗日对偶(Lagrange duality)(转载)
引言:尝试用最简单易懂的描述解释清楚机器学习中会用到的拉格朗日对偶性知识,非科班出身,如有数学专业博友,望多提意见! 1.原始问题 假设是定义在上的连续可微函数(为什么要求连续可微呢,后面再说,这里不 ...
- shell基础(一)
Shell本身是一个用C语言编写的程序,它是用户使用Unix/Linux的桥梁,用户的大部分工作都是通过Shell完成的.Shell既是一种命令语言,又是一种程序设计语言.作为命令语言,它交互式地解释 ...
- linux 下Time_wait过多问题解决
linux 下Time_wait过多问题解决 net.ipv4.tcp_syncookies = 1表示开启SYN Cookies.当出现SYN等待队列溢出时,启用cookies来处理,可防范少量SY ...