ScrollView如何正常響應自己添加的手勢

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

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章