UIPantapgesturerecognizerr怎么识别手势是向左滑还是向右滑的

[解决方案]处理UIScrollView滑动与左右侧栏UIPanGestureRecognizer手势冲突-中国学网-中国IT综合门户网站
[解决方案]处理UIScrollView滑动与左右侧栏UIPanGestureRecognizer手势冲突
来源:互联网 发表时间: 18:14:53 责任编辑:鲁晓倩字体:
在左右侧栏的控制器,我用的是一个UINavigationController的子类LCPanNavigationController,在LCPanNavigationController.m中-&(BOOL)gestureRecognizer:(UIGestureRecognizer&*)gestureRecognizer&shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer&*)otherGestureRecognizer方法中添加以下处理://&如果otherGestureRecognizer是scrollview&判断scrollview的contentOffset&是否小于等于0,YES&cancel&scrollview&的手势,否cancel自己
&&&&if&([[otherGestureRecognizer&view]&isKindOfClass:[UIScrollView&class]])&{
&&&&&&&&UIScrollView&*scrollView&=&(UIScrollView&*)[otherGestureRecognizer&view];
&&&&&&&&if&(scrollView.contentOffset.x&&=&0)&{
&&&&&&&&&&&&
&&&&&&&&&&&&[self&cancelOtherGestureRecognizer:otherGestureRecognizer];
&&&&&&&&&&&&
&&&&&&&&}else{
&&&&&&&&&&&&
&&&&&&&&&&&&[self&cancelOtherGestureRecognizer:self.pan];
&&&&&&&&return&NO;
&&&&}-&(void)cancelOtherGestureRecognizer:(UIGestureRecognizer&*)otherGestureRecognizer
&&&&NSSet&*touchs&=&[self.pan.event&touchesForGestureRecognizer:otherGestureRecognizer];
&&&&[otherGestureRecognizer&touchesCancelled:touchs&withEvent:self.pan.event];
相关文章:
上一篇文章:下一篇文章:
最新添加资讯
24小时热门资讯
Copyright © 2004- All Rights Reserved. 中国学网 版权所有
京ICP备号-1 京公网安备02号& IOS手势总结与UIResponder
IOS手势总结与UIResponder
手势与UITouch的区别:
手势继承自UIGestureRecognizer--&NSObject。
而UITouch直接继承自NSObject。
事实上,手势和UITouch本身关系不大,在IOS3.2之前根本没有手势,以前的所有操作都依赖于UITouch,这也是为什么UITouch定义在UIRsponder中的原因之一,并且所有的UIResponder及其子类都自带监听UITouch的方法。
手势的分类:
一般来说,我们用到的UIGestureRecognizer的子类有以下几种:
1:UITapGestureRecognizer
这个手势也就是常见的点击手势,可以通过设置numberOfTapsRequired来设定需要连续多少次点击才会触发事件。
需要注意的是:
如果在一个view上面同时既要触发单击又要触发双击,这样的情况下直接将两个手势加在这个view上面会导致触发双击事件的时刻,单击事件也会被触发。我们需要设置这样一个属性,来让系统一次只触发双击或者单击事件。
[doubleTap requireGestureRecognizerToFail:tap];
这个方法也可以用于对其它手势的互斥。
numberOfTouchesRequired可以用来设置手势需要多少手指。
2:UIPinchGestureRecognizer
放大缩放手势(放大过程中不断触发)。
需要注意的是,通过pinch.scale得到的数值是相对于原图的缩放比例,所以需要成员变量保存。
pinch.velocity可以得到手势的速率。
3:UIPanGestureRecognizer
拖动手势(拖动过程中不断触发)。
通过[pan translationInView:]返回一个CGpoint,这个Point的内容代表的就是当前鼠标相对于刚开始(按下的位置)的相对坐标。
通过[pan locationInView:self.view]可以得到当前鼠标相对于self.View的相对坐标。(同理可得到其它坐标系下坐标)
4:UILongPressGestureRecognizer
长按手势。
numberOfTapsRequired:长按之前需要先tap几下。
numberOfTouchesRequired:需要几个手指。
minimumPressDuration:最小持续长按时间。
注意:长按事件的监听不像点击事件一样只触发一次,也不像拖动事件一样会不断的触发,这个时间会被触发两次,分别是在长按生效的时刻触发一次,和松开手指的时刻触发一次。可以通过state,进行不同处理。
5:UISwipeGestureRecognizer
轻滑手势,也就是cell中的滑动删除手势。
numberOfTouchesRequired需要几个手指。
@property(nonatomic) UISwipeGestureRecognizerDirection
设置想要监听轻滑的方向。
6:UIRotationGestureRecognizer
旋转手势(持续触发)。
这个手势与pinch类似。两个属性rotation和velocity。
关于父类UIGestureRecognizer:
UIGestureRecognizer是所有手势的根类,所以,它的很多方法都是在手势中能够使用的。
首先是当前手势的状态:
UIGestureRecognizerState。
一般情况下,通过判断这个状态来区分手势在什么阶段。
@property(nonatomic,readonly) UIView *
所有手势都可以通过这个属性获得当前响应手势的view。
可以为所有手势再添加一个触发事件,当这个手势被触发的时刻,原有处理方法触发后马上出发这个方法。(也可以先用init初始化,再添加这个事件监听)。
addTarget:self action:@selector()
delaysTouchesBegan属性会将当前的手势级别提升,也就是说,优先响应这个手势,只有这个手势失败后才会触发其他手势。
delaysTouchesEnded同理,只有这个手势失败后,才结束整个手势识别过程。
- (CGPoint)locationInView:(UIView*)
所有手势其实都可以得到自己的坐标。
- (CGPoint)locationOfTouch:(NSUInteger)touchIndex inView:(UIView*)某个手指的点击的位置(手势开始的时刻的位置))。
------------------如果需要对手势事件响应进行自定义就要用到代理了-------------------------
UIGestureRecognizerDelegate
这个代理包含三个回调方法。
- (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureR
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer*)otherGestureR
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)
UITouch:(包含点击次数和时间等属性。)
参考:/blog/1473287
UITouch的监听方法直接定义在UIResponder中
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)
- (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)
UIResponder:
利用这个类可以实现对touch事件的监听和对设备自身运动motion的监听。
还支持远程事件。
- (void)remoteControlReceivedWithEvent:(UIEvent *)event
参考文章:
http://blog.csdn.net/maojudong/article/details/7798138
关于firstResponder:(UIResponder默认为不能成为第一响应者的,子类覆写canBecomeFirstResponder方法来改变它)
3. 输入视图管理相关:
输入视图是指当对象为 firstResponder 对象时,需要借助另外一个视图用来处理当前对象的信息输入,如 UITextView 和 UITextField 两个对象,在 UITextField 成为 firstResponder 对象时,会显示一个系统键盘,用来输入信息。这个键盘视图就是一个输入视图了。一共有两个相关的输入视图,一个是 inputView, 另一个是 inputAccessoryView,这两个视图显示的关系如下图:
从图中可以看到, 如果 inputView 和 inputAccessoryView 两个属性都指定了相应的视图,则 inputAccessoryView 对象显示在 inputView 对象的上面。
与输入相关的还有一个 reloadInputViews 方法用来重新载入输入视图。
本文固定链接:
[上一篇][下一篇]
最新文章随机精彩热门排行
日志总数:3881 篇
评论总数:22250 评
标签数量:4466 个
链接总数:4 条
建站日期:
运行天数:1060 天iOS手势识别的详细使用_百度文库
两大类热门资源免费畅读
续费一年阅读会员,立省24元!
iOS手势识别的详细使用
上传于||暂无简介
阅读已结束,如果下载本文需要使用0下载券
想免费下载更多文档?
下载文档到电脑,查找使用更方便
还剩12页未读,继续阅读
你可能喜欢iOS手势识别的详细使用:拖动、缩放、旋转、点击、手势依赖、自定义手势 - 博客 - 伯乐在线
& iOS手势识别的详细使用:拖动、缩放、旋转、点击、手势依赖、自定义手势
1、UIGestureRecognizer介绍
手势识别在iOS上非常重要,手势操作移动设备的重要特征,极大的增加了移动设备使用便捷性。
iOS系统在3.2以后,为方便开发这使用一些常用的手势,提供了UIGestureRecognizer类。手势识别UIGestureRecognizer类是个抽象类,下面的子类是具体的手势,开发这可以直接使用这些手势识别。
上面的手势对应的操作是:
Tap(点一下)
Pinch(二指往內或往外拨动,平时经常用到的缩放)
Rotation(旋转)
Swipe(滑动,快速移动)
Pan (拖移,慢速移动)
LongPress(长按)
UIGestureRecognizer的继承关系如下:
2、使用手势的步骤
使用手势很简单,分为两步:
创建手势实例。当创建手势时,指定一个回调方法,当手势开始,改变、或结束时,回调方法被调用。
添加到需要识别的View中。每个手势只对应一个View,当屏幕触摸在View的边界内时,如果手势和预定的一样,那就会回调方法。
ps:一个手势只能对应一个View,但是一个View可以有多个手势。
建议在真机上运行这些手势,模拟器操作不太方便,可能导致你认为手势失效。
3、Pan 拖动手势:
UIImageView *snakeImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@&snake.png&]];
snakeImageView.frame = CGRectMake(50, 50, 100, 160);
UIPanGestureRecognizer *panGestureRecognizer = [[UIPanGestureRecognizer alloc]
initWithTarget:self
action:@selector(handlePan:)];
[snakeImageView addGestureRecognizer:panGestureRecognizer];
[self.view setBackgroundColor:[UIColor whiteColor]];
[self.view addSubview:snakeImageView];
新建一个ImageView,然后添加手势
回调方法:
- (void) handlePan:(UIPanGestureRecognizer*) recognizer
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];
4、Pinch缩放手势
UIPinchGestureRecognizer *pinchGestureRecognizer = [[UIPinchGestureRecognizer alloc]
initWithTarget:self
action:@selector(handlePinch:)];&p class=&p1&&[&span class=&s1&&snakeImageView&/span& &span class=&s2&&addGestureRecognizer&/span&:pinchGestureRecognizer];&/p&
- (void) handlePinch:(UIPinchGestureRecognizer*) recognizer
recognizer.view.transform = CGAffineTransformScale(recognizer.view.transform, recognizer.scale, recognizer.scale);
recognizer.scale = 1;
5、Rotation旋转手势
UIRotationGestureRecognizer *rotateRecognizer = [[UIRotationGestureRecognizer alloc]
initWithTarget:self
action:@selector(handleRotate:)];
[snakeImageView addGestureRecognizer:rotateRecognizer];
- (void) handleRotate:(UIRotationGestureRecognizer*) recognizer
recognizer.view.transform = CGAffineTransformRotate(recognizer.view.transform, recognizer.rotation);
recognizer.rotation = 0;
添加了这几个手势后,运行看效果,程序中的imageView放了一个
\____|__________/
_—-_
~–______-~
的图片,在模拟器上拖动是没问题的。缩放和旋转有点问题,估计是因为在模拟器上的模拟的两个接触点距离在imageView的边界外了,所以操作无效果。
建议在真机上运行这个手势。
在模拟器上缩放和选择的操作技巧:
可以把imageView的frame值设置大一点,按住alt键,按下触摸板(不按下不行),这样就可以旋转和缩放了。
6、添加第二个ImagView并添加手势
记住:一个手势只能添加到一个View,两个View当然要有两个手势的实例了
- (void)viewDidLoad
[super viewDidLoad];
UIImageView *snakeImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@&snake.png&]];
UIImageView *dragonImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@&dragon.png&]];
snakeImageView.frame = CGRectMake(120, 120, 100, 160);
dragonImageView.frame = CGRectMake(50, 50, 100, 160);
[self.view addSubview:snakeImageView];
[self.view addSubview:dragonImageView];
for (UIView *view in self.view.subviews) {
UIPanGestureRecognizer *panGestureRecognizer = [[UIPanGestureRecognizer alloc]
initWithTarget:self
action:@selector(handlePan:)];
UIPinchGestureRecognizer *pinchGestureRecognizer = [[UIPinchGestureRecognizer alloc]
initWithTarget:self
action:@selector(handlePinch:)];
UIRotationGestureRecognizer *rotateRecognizer = [[UIRotationGestureRecognizer alloc]
initWithTarget:self
action:@selector(handleRotate:)];
[view addGestureRecognizer:panGestureRecognizer];
[view addGestureRecognizer:pinchGestureRecognizer];
[view addGestureRecognizer:rotateRecognizer];
[view setUserInteractionEnabled:YES];
[self.view setBackgroundColor:[UIColor whiteColor]];
多添加了一条龙的view,两个view都能接收上面的三种手势。运行效果如下:
7、拖动(pan手势)速度(以较快的速度拖放后view有滑行的效果)
如何实现呢?
监视手势是否结束
监视触摸的速度
- (void) handlePan:(UIPanGestureRecognizer*) recognizer
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];
if (recognizer.state == UIGestureRecognizerStateEnded) {
CGPoint velocity = [recognizer velocityInView:self.view];
CGFloat magnitude = sqrtf((velocity.x * velocity.x) + (velocity.y * velocity.y));
CGFloat slideMult = magnitude / 200;
NSLog(@&magnitude: %f, slideMult: %f&, magnitude, slideMult);
float slideFactor = 0.1 * slideM // Increase for more of a slide
CGPoint finalPoint = CGPointMake(recognizer.view.center.x + (velocity.x * slideFactor),
recognizer.view.center.y + (velocity.y * slideFactor));
finalPoint.x = MIN(MAX(finalPoint.x, 0), self.view.bounds.size.width);
finalPoint.y = MIN(MAX(finalPoint.y, 0), self.view.bounds.size.height);
[UIView animateWithDuration:slideFactor*2 delay:0 options:UIViewAnimationOptionCurveEaseOut animations:^{
recognizer.view.center = finalP
} completion:nil];
代码实现解析:
计算速度向量的长度(估计大部分都忘了)这些知识了。
如果速度向量小于200,那就会得到一个小于的小数,那么滑行会很短
基于速度和速度因素计算一个终点
确保终点不会跑出父View的边界
使用UIView动画使view滑动到终点
运行后,快速拖动图像view放开会看到view还会在原来的方向滑行一段路。
8、同时触发两个view的手势
手势之间是互斥的,如果你想同时触发蛇和龙的view,那么需要实现协议UIGestureRecognizerDelegate,
@interface ViewController : UIViewController&UIGestureRecognizerDelegate&
并在协议这个方法里返回YES。
-(BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer
return YES;
把self作为代理设置给手势:
panGestureRecognizer.delegate =
pinchGestureRecognizer.delegate =
rotateRecognizer.delegate =
这样可以同时拖动或旋转缩放两个view了。
9、tap点击手势
这里为了方便看到tap的效果,当点击一下屏幕时,播放一个声音。
为了播放声音,我们加入AVFoundation.framework这个框架。
- (AVAudioPlayer *)loadWav:(NSString *)filename {
NSURL * url = [[NSBundle mainBundle] URLForResource:filename withExtension:@&wav&];
AVAudioPlayer * player = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:&error];
if (!player) {
NSLog(@&Error loading %@: %@&, url, error.localizedDescription);
[player prepareToPlay];
我会在最后例子代码给出完整代码,添加手势的步骤和前面一样的。
#import &UIKit/UIKit.h&
#import &AVFoundation/AVFoundation.h&
@interface ViewController : UIViewController&UIGestureRecognizerDelegate&
@property (strong) AVAudioPlayer * chompP
@property (strong) AVAudioPlayer * heheP
- (void)handleTap:(UITapGestureRecognizer *)recognizer {
[self.chompPlayer play];
运行,点一下某个图,就会播放一个咬东西的声音。
不过这个点击播放声音有点缺陷,就是在慢慢拖动的时候也会播放。这使得两个手势重合了。怎么解决呢?使用手势的:requireGestureRecognizerToFail方法。
10、手势的依赖性
在viewDidLoad的循环里添加这段代码:
[tapRecognizer requireGestureRecognizerToFail:panGestureRecognizer];
意思就是,当如果pan手势失败,就是没发生拖动,才会出发tap手势。这样如果你有轻微的拖动,那就是pan手势发生了。tap的声音就不会发出来了。
11、自定义手势
自定义手势继承:UIGestureRecognizer,实现下面的方法:
– touchesBegan:withEvent:
– touchesMoved:withEvent:
– touchesEnded:withEvent:
- touchesCancelled:withEvent:
新建一个类,继承UIGestureRecognizer,代码如下:
#import &UIKit/UIKit.h&
typedef enum {
DirectionUnknown = 0,
DirectionLeft,
DirectionRight
@interface HappyGestureRecognizer : UIGestureRecognizer
@property (assign) int tickleC
@property (assign) CGPoint curTickleS
@property (assign) Direction lastD
#import &HappyGestureRecognizer.h&
#import &UIKit/UIGestureRecognizerSubclass.h&
#define REQUIRED_TICKLES
#define MOVE_AMT_PER_TICKLE
@implementation HappyGestureRecognizer
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch * touch = [touches anyObject];
self.curTickleStart = [touch locationInView:self.view];
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
// Make sure we've moved a minimum amount since curTickleStart
UITouch * touch = [touches anyObject];
CGPoint ticklePoint = [touch locationInView:self.view];
CGFloat moveAmt = ticklePoint.x - self.curTickleStart.x;
Direction curD
if (moveAmt & 0) {
curDirection = DirectionL
curDirection = DirectionR
if (ABS(moveAmt) & MOVE_AMT_PER_TICKLE)
// 确认方向改变了
if (self.lastDirection == DirectionUnknown ||
(self.lastDirection == DirectionLeft && curDirection == DirectionRight) ||
(self.lastDirection == DirectionRight && curDirection == DirectionLeft)) {
// 挠痒次数
self.tickleCount++;
self.curTickleStart = tickleP
self.lastDirection = curD
// 一旦挠痒次数超过指定数,设置手势为结束状态
// 这样回调函数会被调用。
if (self.state == UIGestureRecognizerStatePossible && self.tickleCount & REQUIRED_TICKLES) {
[self setState:UIGestureRecognizerStateEnded];
- (void)reset {
self.tickleCount = 0;
self.curTickleStart = CGPointZ
self.lastDirection = DirectionU
if (self.state == UIGestureRecognizerStatePossible) {
[self setState:UIGestureRecognizerStateFailed];
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
[self reset];
- (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event
[self reset];
调用自定义手势和上面一样,回到这样写:
- (void)handleHappy:(HappyGestureRecognizer *)recognizer{
[self.hehePlayer play];
手势成功后播放呵呵笑的声音。
在真机上运行,按住某个view,快速左右拖动,就会发出笑的声音了。
代码解析:
先获取起始坐标:curTickleStart
通过和ticklePoint的x值对比,得出当前的放下是向左还是向右。再算出移动的x的值是否比MOVE_AMT_PER_TICKLE距离大,如果太则返回。
再判断是否有三次是不同方向的动作,如果是则手势结束,回调。
参考:/6567/uigesturerecognizer-tutorial-in-ios-5-pinches-pans-and-more
例子代码:
可能感兴趣的话题
关于伯乐在线博客
在这个信息爆炸的时代,人们已然被大量、快速并且简短的信息所包围。然而,我们相信:过多“快餐”式的阅读只会令人“虚胖”,缺乏实质的内涵。伯乐在线博客团队正试图以我们微薄的力量,把优秀的原创/译文分享给读者,做一个小而精的精选博客,为“快餐”添加一些“营养”元素。
新浪微博:
推荐微信号
(加好友请注明来意)
– 好的话题、有启发的回复、值得信赖的圈子
– 分享和发现有价值的内容与观点
– 为IT单身男女服务的征婚传播平台
– 优秀的工具资源导航
– 翻译传播优秀的外文文章
– 国内外的精选文章
– UI,网页,交互和用户体验
– 专注iOS技术分享
– 专注Android技术分享
– JavaScript, HTML5, CSS
– 专注Java技术分享
– 专注Python技术分享
& 2016 伯乐在线
赞助云主机

我要回帖

更多关于 speechrecognizer 的文章

 

随机推荐