ScrollView如何正常响应自己添加的手势
来源:互联网 发布:阿里云 共享存储 编辑:程序博客网 时间:2024/06/06 17:34
UIScrollView自带了一些手势,有时候可能会与scrollView上面的子view的手势产生冲突。
个人测试了一下,发现在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
1 0
- ScrollView如何正常响应自己添加的手势
- ios scrollview 滚动后 的 subviews 比自己正常添加的 多了2个
- 如何给scrollview上添加的子控件UIView上添加手势
- UIImageView添加手势不响应的原因
- 手势识别(三)2种添加手势方法和如何响应手势
- UIimageview添加手势,无法响应手势事件
- 在UITableView或UICollectionView的自定义的cell里添加手势或button,响应方法时如何传参数
- UIWebView中添加手势不响应的解决方案
- 控制只添加另一个控制器的view,手势不响应
- 如何解决listView或scrollView+viewpager手势冲突的问题
- UITableView的上下滑动与自己添加的手势冲突
- 高德地图添加手势不响应
- 自定义cell,添加uilable,并且给lable添加tap手势,不响应的问题,对应解决方法
- scrollview与手势冲突如何解决
- scrollview与手势冲突如何解决
- 使ScrollView中的控件获得焦点,能正常响应触摸
- 手势影响view的响应
- 常见手势的响应事件
- Topic On ARM Core
- float转换为二进制
- 实习生刚入职 两周总结
- KMP算法
- Volley的使用
- ScrollView如何正常响应自己添加的手势
- Spring 之AOP AspectJ切入点语法详解(最全了,不需要再去其他地找了)---zhangkaitao
- 【设计配色宝典】设计师必备七色配色宝典,附AI源文件!
- java nio aio bio
- Andriod的活动生存期
- git入门
- jQuery 的.serialize()
- ansi格式的TXT字符串在ios如何解析
- Raspberry Pi超声波测距样例