您的位置:首页 > 其它

ScrollView如何正常响应自己添加的手势

2016-03-11 09:43 411 查看
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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: