ScrollView如何正常响应自己添加的手势
2016-03-11 09:43
411 查看
UIScrollView自带了一些手势,有时候可能会与scrollView上面的子view的手势产生冲突。
个人测试了一下,发现在scrollView上面的子view上的手势响应优先级是比scrollView上的高的,只是touchesBegan这些自带的方法需要判断一下。
下面是一个自定义的view,该view上面其实是有两种手势的,tap和pan,还有重写的touchesBegan。等会将其添加到自定义的scrollView上。
@interface ViewCustom()<UIGestureRecognizerDelegate>{ UITapGestureRecognizer *_tap; UIPanGestureRecognizer *_pan;}@end@implementation ViewCustom- (instancetype)initWithFrame:(CGRect)frame { self = [super initWithFrame:frame]; if (self) {// _tap = [[UITapGestureRecognizer
alloc]initWithTarget:self action:@selector(tapAction:)];// [self addGestureRecognizer:_tap]; _pan = [[UIPanGestureRecognizer alloc]initWithTarget:self action:@selector(panAction:)]; [self addGestureRecognizer:_pan]; } return self;}- (void)tapAction:(UITapGestureRecognizer
*)sender { UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"就让回忆永远停在那里" message:@"_viewCustom" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil]; [alert show];}- (void)panAction:(UIPanGestureRecognizer *)sender { UIAlertView *alert = [[UIAlertView
alloc]initWithTitle:@"这样的节奏,谁都无可奈何" message:@"_viewCustom" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil]; [alert show];}- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event { NSLog(@"一件黑色毛衣,两个人的回忆。");}@end
下面是自定义的scrollView,下面这个方法是优先响应子view的touchesBegan方法。
个人测试了一下,发现在scrollView上面的子view上的手势响应优先级是比scrollView上的高的,只是touchesBegan这些自带的方法需要判断一下。
下面是一个自定义的view,该view上面其实是有两种手势的,tap和pan,还有重写的touchesBegan。等会将其添加到自定义的scrollView上。
// // ViewCustom.m // ScrollView // // Created by Wu on 16/3/17. // Copyright © 2016年 Wu. All rights reserved. // #import "ViewCustom.h"<pre name="code" class="objc">// // ScroolViewCustom.m // ScrollView // // Created by Wu on 16/3/17. // Copyright © 2016年 Wu. All rights reserved. // #import "ScrollViewCustom.h" #import "ViewCustom.h" @implementation ScrollViewCustom - (BOOL)touchesShouldBegin:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event inContentView:(UIView *)view { if ([view isKindOfClass:[ViewCustom class]]) { return YES;//响应当前接触的view的TouchBegin } else { return NO; } } @end
@interface ViewCustom()<UIGestureRecognizerDelegate>{ UITapGestureRecognizer *_tap; UIPanGestureRecognizer *_pan;}@end@implementation ViewCustom- (instancetype)initWithFrame:(CGRect)frame { self = [super initWithFrame:frame]; if (self) {// _tap = [[UITapGestureRecognizer
alloc]initWithTarget:self action:@selector(tapAction:)];// [self addGestureRecognizer:_tap]; _pan = [[UIPanGestureRecognizer alloc]initWithTarget:self action:@selector(panAction:)]; [self addGestureRecognizer:_pan]; } return self;}- (void)tapAction:(UITapGestureRecognizer
*)sender { UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"就让回忆永远停在那里" message:@"_viewCustom" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil]; [alert show];}- (void)panAction:(UIPanGestureRecognizer *)sender { UIAlertView *alert = [[UIAlertView
alloc]initWithTitle:@"这样的节奏,谁都无可奈何" message:@"_viewCustom" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil]; [alert show];}- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event { NSLog(@"一件黑色毛衣,两个人的回忆。");}@end
下面是自定义的scrollView,下面这个方法是优先响应子view的touchesBegan方法。
// // ScroolViewCustom.m // ScrollView // // Created by Wu on 16/3/17. // Copyright © 2016年 Wu. All rights reserved. // #import "ScrollViewCustom.h" #import "ViewCustom.h" @implementation ScrollViewCustom - (BOOL)touchesShouldBegin:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event inContentView:(UIView *)view { if ([view isKindOfClass:[ViewCustom class]]) { return YES;//响应当前接触的view的TouchBegin } else { return NO; } } @end下面是调用,如果把_scrollView换成UIScrollView对象,你会发现代码还是会优先响应子view上的手势。你可以将自定义的scrollView里面的方法的YES和NO调换。会发现不会响应子view的touchesBegan的方法。
// // ViewController.m // ScrollView // // Created by Wu on 16/3/16. // Copyright © 2016年 Wu. All rights reserved. // #import "ViewController.h" #import "ScrollViewCustom.h" #import "ViewCustom.h" @interface ViewController () { ScrollViewCustom *_scrollView; ViewCustom *_viewCustom; } @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; //添加一个自定义的scroolView到view上面 _scrollView = [[ScrollViewCustom alloc]initWithFrame:[UIScreen mainScreen].bounds]; _scrollView.delaysContentTouches = NO;//注意设定这个属性 [self.view addSubview:_scrollView]; _scrollView.backgroundColor = [UIColor yellowColor]; _scrollView.scrollEnabled = YES; //添加自定义的view _viewCustom = [[ViewCustom alloc]initWithFrame:CGRectMake(20, 20, 200, 800)]; _viewCustom.backgroundColor = [UIColor purpleColor]; [_scrollView addSubview:_viewCustom]; _scrollView.contentSize = _viewCustom.bounds.size; } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; } @end
相关文章推荐
- WGS84经纬度坐标到北京54高斯投影坐标的转换
- postfix+dovecot+roundcube使用客户端给外域发送邮件提示Relay access denied
- Spring 之AOP AspectJ切入点语法详解(最全了,不需要再去其他地找了)---zhangkaitao
- KMP算法
- C++之:友元类
- 伪分布式网络爬虫框架的设计与自定义实现(一)
- 0311 今天不开心就做了个新的 复利计算
- 数据库建表原则
- 高效工作
- 深入剖析设计模式中的组合模式应用及在C++中的实现
- 【代码笔记】给UIImageView加上圆角效果
- 欧拉项目第7题 10001st prime
- java如何写一个防止同账号不同ip登录的程序?
- Openstack 二次开发之:在windows 环境下编译Openstack-java-sdk
- KMP算法(快速模式匹配)
- 【代码笔记】给UIImageView加上圆角效果
- 0311单利复利
- Java内存简介
- Educational Codeforces Round 7 Codeforces 622D Codeforces 622E
- php curl模拟登陆