获取ios 手势控件点击哪个控件 UIGestureRecognizer

ios之UIGestureRecognizer手势基础使用解析 - 推酷
ios之UIGestureRecognizer手势基础使用解析
UIGestureRecognizer 的子类分别有很多手势,通过 不用的手势可以执行不同的操作,下面来介绍下他们的基本使用方法所有手势配置基本相同,只是针对不同的手势里面有部分属性可以设置,比如说tap点进去看他有两个参数可以设置一个是点击次数,和点击手指数可设置。如果不知道这个手势能配置说明参数,那么点击进入相应的.h 文件查看
UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(processGestureRecongnizer:)];//点击手势
[tapGesture setNumberOfTouchesRequired:1];
[tapGesture setNumberOfTapsRequired:1];
[_view addGestureRecognizer:tapGesture];
[tapGesture release];
- (void)processGestureRecongnizer:(UIGestureRecognizer *)gesture
if ([gesture isKindOfClass:[UITapGestureRecognizer class]]) {
[self positionAnimation];
#pragma mark -- InitUserInterface
- (void)initUserInterface
_view = [[UIView alloc]init];
[_view setBounds:CGRectMake(0, 0, 200, 200)];
[_view setCenter:CGPointMake(CGRectGetMidX(self.view.bounds), CGRectGetMidY(self.view.bounds))];
[_view setBackgroundColor:[UIColor grayColor]];
[self.view addSubview:_view];
//两手指拨动手势
UIPinchGestureRecognizer *pinch = [[UIPinchGestureRecognizer alloc]initWithTarget:self action:@selector(processGestureRecognizer:)];
[pinch setDelegate:self];//设置代理
[_view addGestureRecognizer:pinch]; //对view添加这个手势
[pinch release];
//旋转手势
UIRotationGestureRecognizer *rotation = [[UIRotationGestureRecognizer alloc]initWithTarget:self action:@selector(processGestureRecognizer:)];
[rotation setDelegate:self];
[_view addGestureRecognizer:rotation];
[rotation release];
//长按手势
UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc]initWithTarget:self action:@selector(processGestureRecognizer:)];
longPress.minimumPressDuration = 2;
[_view addGestureRecognizer:longPress];
[longPress release];
//滑动手势--左
UISwipeGestureRecognizer *leftSwipe = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(processGestureRecognizer:)];
[leftSwipe setDirection:UISwipeGestureRecognizerDirectionLeft];//配置滑动方向
[self.view addGestureRecognizer:leftSwipe];
[leftSwipe release];
//滑动手势--右
UISwipeGestureRecognizer *rightSwipe = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(processGestureRecognizer:)];
[rightSwipe setDirection:UISwipeGestureRecognizerDirectionRight];
[self.view addGestureRecognizer:rightSwipe];
[rightSwipe release];
//拖移手势
UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc]initWithTarget:self action:@selector(processGestureRecognizer:)];
[pan setDelegate:self];
[_view addGestureRecognizer:pan];
[pan release];
#pragma mark -- GestureRrecognizer methods
//代理方法
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldBeRequiredToFailByGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer
return NO;
// 手势action 让对应的手势执行相应地操作
- (void)processGestureRecognizer:(UIGestureRecognizer *)gesture
//判断手势类型
if ([gesture isKindOfClass:[UIPinchGestureRecognizer class]])
UIPinchGestureRecognizer *pinch = (UIPinchGestureRecognizer *)
static float lastS//静态变量记录上次大小
if (pinch.state == UIGestureRecognizerStateBegan) {
lastScale = pinch.// 手势开始把初始scale赋给静态变量方便更新
}else if (pinch.state == UIGestureRecognizerStateChanged){
[_view setTransform:CGAffineTransformScale(_view.transform, 1+(pinch.scale - lastScale), 1+(pinch.scale - lastScale))];//让View进行动态的放大或缩小
lastScale = pinch.// 更新scale的值 --(这样做让view保持变化后的状态而不会是初始状态)
//此方法不需要更新lastScale的值
都是从原型开始
[_view setTransform:CGAffineTransformMakeScale(1+(pinch.scale - lastScale), 1+(pinch.scale - lastScale))];
}else if ([gesture isKindOfClass:[UIRotationGestureRecognizer class]])
UIRotationGestureRecognizer *rotation = (UIRotationGestureRecognizer *)
static float lastR
if (rotation.state == UIGestureRecognizerStateBegan) {
lastRotation = rotation.
}else if (rotation.state == UIGestureRecognizerStateChanged){
[_view setTransform:CGAffineTransformRotate(_view.transform, rotation.rotation - lastRotation)];
lastRotation = rotation.
else if ([gesture isKindOfClass:[UILongPressGestureRecognizer class]])
UILongPressGestureRecognizer *longPress = (UILongPressGestureRecognizer *)
if (longPress.state == UIGestureRecognizerStateBegan) {
UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@&温馨提示& message:@&Long Press Begin& delegate:self cancelButtonTitle:@&确定& otherButtonTitles: nil];
[alert show];
[alert release];
else if ([gesture isKindOfClass:[UISwipeGestureRecognizer class]])
UISwipeGestureRecognizer *swipe = (UISwipeGestureRecognizer *)
if (swipe.direction == UISwipeGestureRecognizerDirectionLeft) {
[UIView animateWithDuration:1 animations:^{
[_view setCenter:CGPointMake(CGRectGetMinX(self.view.bounds), CGRectGetMidY(self.view.bounds))];
[UIView animateWithDuration:1 animations:^{
[_view setCenter:CGPointMake(CGRectGetMaxX(self.view.bounds), CGRectGetMidY(self.view.bounds))];
else if ([gesture isKindOfClass:[UIPanGestureRecognizer class]])
UIPanGestureRecognizer *pan = (UIPanGestureRecognizer *)
static CGPoint lastL
if (pan.state == UIGestureRecognizerStateBegan) {
lastLocation = _view.
}else if (pan.state == UIGestureRecognizerStateChanged)
CGPoint translationPoint = [pan translationInView:self.view];
_view.center = CGPointMake(translationPoint.x + lastLocation.x, translationPoint.y+ lastLocation.y);
}else if (pan.state == UIGestureRecognizerStateEnded)
lastLocation = CGPointZ
已发表评论数()
&&登&&&录&&
已收藏到推刊!
请填写推刊名
描述不能大于100个字符!
权限设置: 公开
仅自己可见UIScrollView子控件加UIPanGestureRecognizer拖动手势
编辑:www.fx114.net
本篇文章主要介绍了"UIScrollView子控件加UIPanGestureRecognizer拖动手势",主要涉及到UIScrollView子控件加UIPanGestureRecognizer拖动手势方面的内容,对于UIScrollView子控件加UIPanGestureRecognizer拖动手势感兴趣的同学可以参考一下。
问题描述:横向UIScrollView里面的子控件,我使用了UIPanGestureRecognizer来实现拖动,但是发现UIScrollView无法响应滚动事件,因为scroll里面布满了我放的控件,所以不能响应。如果先让scroll响应
[panGestureRecognizer requireGestureRecognizerToFail:sroll.panGestureRecognizer]//先处理scroll发现,我添加的手势又不响应了,因为sroll滚动每次都成功。
解决办法:后来在 栈溢出 问答平台找到了一个没有采纳的答案解决了问题,使用手势代理,通过
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer来解决
这个方法是用来处理多手势共存的,返回NO则响应一个手势,返回YES为同时响应,我的处理方法是,判断当前gestureRecognizer是否是panGestureRecognizer,然后判断方向,如果为上下方向则返回NO,则scroll不响应,否则都响应,问题完美解决。
当然拖动手势没法直接获取方向需要通过
CGPoint translation = [(UIPanGestureRecognizer*)gestureRecognizer translationInView:self];来获得,如果translation.y的绝对值大于translation.x的绝对值就可以看成是上下方向。
本文标题:
本页链接:下次自动登录
现在的位置:
& 综合 & 正文
用IB(interface builder)添加 UIGestureRecognizer的步骤
很多搞ios开发的朋友习惯在里完成所有工作,而且各自都有这样做的理由,但是我觉得IB也是有存在的道理的,他能使我们的代码简洁,当然你必须还得花时间熟悉IB的使用方法,这个也许会让人纠结。
项目中要用到手势,先前是在代码里直接做的,今天想尝试一下用IB来做,google了一下,搜出不多几条有用的信息,参照着弄了一下最后搞出来了,现在写这个的目的就是再梳理一下,顺便也是熟悉一下IB,因为也是刚刚接触ios,很多反复的熟悉也是必要的,如果能对其他朋友有帮助那就更好了。技术这个东西,探索和尝试的过程是比较艰辛的,一但再回头看就觉得很简单了。下面我以我的项目为例,说一下用IB为控件添加手势的步骤。
1.拖拽一个UIImageView和一个Tap Gesture Recognizer到根View里;效果如下图,里面的输入框是我项目里用到的,和咱们今天要说的没关系,忽略它好了。
2.在.h文件里定义UIImageView 和UITapGestureRecognizer并和IB里相应的控件关联起来;关联就是拖拽了,语言也不是很好描述,gif图最擅长这个,如果刚接触IB的朋友,多胡乱拖拽几次 慢慢会有体会的 。
IBOutletUIImageView*
IBOutletUITapGestureRecognizer* tapR
关联后IB里基本上是下面这个样子,其他控件不用管,我们只关心avatar和tapRecognizer
3.但是这时候 UIImageView和UITapGestureRecognizer还没有建立起关系,我们直接拖拽左侧的UITapGestureRecognizer到右边的UIImageView。
完毕后右击UITapGestureRecognizer发现Referencing Outlet Collections 和ImageView建立起了连接。如下图
4.这时候,需要我们在代码文件里定义tap后的响应函数
-(IBAction)tapAvatar:(id)
然后在IB里右击File‘s Owner 我们看到刚才添加的tapAvatar列在了最下面(Received Actions里)
5.选中tapAvatar拉到左侧的Tap Gesture Recognizer建立连接,如下图
这样用IB添加一个手势的步骤完成了,运行一下代码 点击这个UIImageView就会执行-(IBAction)tapAvatar:(id)sender了。写了这些不知道是否准确的说明白了这个问题,希望这个东西对需要的人有所帮助。
&&&&推荐文章:
【上篇】【下篇】
<a href="/zt/.html" title="<html:optionscollection下次自动登录
现在的位置:
& 综合 & 正文
IOS 使用自定义手势屏蔽按钮解决方法/UITapGestureRecognizer屏蔽Button
0、有时候需要在scrollview上面添加手势,让其获取点击后,收起键盘,就用到添加手势。可是会出现一些后续问题,比如会拦截scrollview上面的其他一切可点击的控件,你可能会去设置手势控件的一些属性。但仅仅设置 UIGestureRecognizer 的属性是不够的,比如 :
screenTouch.numberOfTapsRequired =1;
[screenTouchsetCancelsTouchesInView:NO];
这种有时候会拦截UITextField上面的清空text的按钮。这时候就需要下面这种方法,从根本上处理好手势响应事件方法。
头文件中添加&UIGestureRecognizerDelegate&委托
2、在.m体文件中添加实现方法————
// 当一个事件判定为是一个手势时,先进入手势代理方法。该方法中判断点击的视图是不是button如果是button则返回NO,手势不响应该事件,既然没响应,也不会取消该事件的传递。则button会接收到该事件,并作处理。
#pragma mark - UIGestureRecognizerDelegate
-(BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch
// 判断是不是UIButton的类
if ([touch.view isKindOfClass:[UIButton class]])
return NO;
return YES;
3、经过这么一次折腾,最大的感受就是,凡事都有解决的办法,不要轻易放弃,要多搜网络资源。
&&&&推荐文章:
【上篇】【下篇】IOS开发之手势&&UIGestureRecognizer 共存
&在 iPhone 或 iPad 的开发中,除了用&touchesBegan / touchesMoved / touchesEnded&这组方法来控制使用者的手指触控外,也可以用&&的衍生类別来进行判断。用&UIGestureRecognizer&的好处在于有现成的手势,开发者不用自己计算手指移动轨迹。UIGestureRecognizer的衍生类別有以下几种:
UITapGestureRecognizer
UIPinchGestureRecognizer
UIRotationGestureRecognizer
UISwipeGestureRecognizer
UIPanGestureRecognizer
UILongPressGestureRecognizer
从命名上不难了解這些类別所对应代表的手势,分別是 Tap(点一下)、Pinch(二指往內或往外拨动)、Rotation(旋转)、Swipe(滑动,快速移动)、Pan (拖移,慢速移动)以及 LongPress(长按)。這些手势別在使用上也很简单,只要在使用前定义并添加到对应的视图上即可。
// 定义一个 recognizer, 并加到需要偵測该手势的 UIView 元件上- (void)viewDidLoad {
UISwipeGestureRecognizer*
// handleSwipeFrom 是偵測到手势,所要呼叫的方法
recognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:selfaction:@selector(handleSwipeFrom)];
// 不同的 Recognizer 有不同的实体变数
// 例如 SwipeGesture 可以指定方向
// 而 TapGesture 則可以指定次數
recognizer.direction = UISwipeGestureRecognizerDirectionUp
[self.view addGestureRecognizer:recognizer];
[recognizer release]; }
- (void)handleSwipeFrom:(UISwipeGestureRecognizer*)recognizer {
// 触发手勢事件后,在这里作些事情
// 底下是刪除手势的方法
[self.view removeGestureRecognizer:recognizer]; }
问题來了。有些手势其实是互相关联的,例如 Tap 与 LongPress、Swipe与 Pan,或是 Tap 一次与Tap 兩次。当一個 UIView 同时添加兩个相关联的手势时,到底我这一下手指头按的要算是 Tap 还是 LongPress?如果照預设作法来看,只要「先滿足条件」的就会跳出并呼叫对应方法,举例来说,如果同时注册了 Pan 和 Swipe,只要手指头一移动就会触发 Pan 然后跳出,因而永远都不會发生 Swipe;单点与双点的情形也是一样,永远都只会触发单点,不會有双点。那么这个问题有解吗?答案是肯定的,UIGestureRecognizer&有个方法叫做requireGestureRecognizerToFail,他可以指定某一个 recognizer,即便自己已经滿足條件了,也不會立刻触发,会等到该指定的 recognizer 确定失败之后才触发。以同时支持单点与双点的手势为例,代码如下:
- (void)viewDidLoad {
// 单击的 Recognizer
UITapGestureRecognizer* singleR
singleRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:selfaction:@selector(handleSingleTapFrom)];
singleTapRecognizer.numberOfTapsRequired = 1; // 单击
[self.view addGestureRecognizer:singleRecognizer];
// 双击的 Recognizer
UITapGestureRecognizer* double;
doubleRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:selfaction:@selector(handleDoubleTapFrom)];
doubleTapRecognizer.numberOfTapsRequired = 2; // 双击
[self.view addGestureRecognizer:doubleRecognizer];
// 关键在这一行,如果双击确定偵測失败才會触发单击
[singleRecognizer requireGestureRecognizerToFail:doubleRecognizer];
[singleRecognizer release];
[doubleRecognizer release];}
阅读(...) 评论()}

我要回帖

更多关于 ios 获取手势点击位置 的文章

更多推荐

版权声明:文章内容来源于网络,版权归原作者所有,如有侵权请点击这里与我们联系,我们将及时删除。

点击添加站长微信