您现在的位置: IOS教程網 >> IOS系統 >  >> iOS 程式開發 >> iOS 開發綜合

iOS 響應鏈(Responder Chain)深入理解與程式碼實戰(二)

編輯:iOS 開發綜合

響應鏈機制是開發中非常重要的概念。只有深入理解事件的傳遞過程,我們才能更好地掌控事件處理。本篇我們繼續研究響應鏈,並實作一個很實用的功能:穿透頂部視圖,讓底部視圖來響應點擊事件。示例程式碼已上傳至 https://github.com/chenyufeng1991/HitTest,可下載調試。最常使用的方法為 hitTest:withEvent: 與 pointInside:withEvent:。

【重寫子視圖】

灰色區域是底部視圖,黃色區域是頂部視圖,我分別為兩者加了點擊手勢。透過重寫 hitTest:withEvent:,無論點擊黃色還是灰色區域,都由底部視圖來響應。其中黃色區域是灰色區域的子視圖。

(1)自訂頂部黃色視圖,繼承自 UIView 並重寫 hitTest:

#import "RewriteView.h"
@implementation RewriteView
- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event {
    UIView *view = [super hitTest:point withEvent:event];
    if (view == self) {
        return nil;
    }
    return [super hitTest:point withEvent:event];
}
@end

程式碼說明:當 view == self 時,代表點擊落在 self(黃色 RewriteView)上,此時回傳 nil,表示自己不響應,交給父視圖(灰色區域)處理。

(2)VC 中的實作:

#import "RewriteTopViewController.h"
#import "Masonry.h"
#import "RewriteView.h"
@interface RewriteTopViewController ()
@end
@implementation RewriteTopViewController
- (void)viewDidLoad {
    [super viewDidLoad];
    UIView *bottomView = [[UIView alloc] initWithFrame:CGRectMake(100, 100, 200, 200)];
    bottomView.backgroundColor = [UIColor grayColor];
    bottomView.tag = 101;
    [self.view addSubview:bottomView];
    [bottomView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.center.equalTo(self.view);
        make.width.equalTo(@200);
        make.height.equalTo(@200);
    }];
    UITapGestureRecognizer *bottomTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapBottomView:)];
    [bottomView addGestureRecognizer:bottomTap];
    RewriteView *topView = [[RewriteView alloc] initWithFrame:CGRectMake(100, 100, 100, 100)];
    topView.backgroundColor = [UIColor yellowColor];
    topView.tag = 102;
    [bottomView addSubview:topView];
    [topView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.center.equalTo(bottomView);
        make.height.equalTo(@100);
        make.width.equalTo(@100);
    }];
    UITapGestureRecognizer *topTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapTopView:)];
    [topView addGestureRecognizer:topTap];
}
- (void)tapBottomView:(id)sender { NSLog(@"%s",__FUNCTION__); }
- (void)tapTopView:(id)sender { NSLog(@"%s",__FUNCTION__); }
@end

注意 topView 由 RewriteView 定義,bottomView 是普通 UIView。執行後你會發現所有點擊都落到灰色底部區域,相當於「穿透」了黃色區域。


下面補充響應鏈的核心原理:

1. 對於觸摸事件,iOS 用 hit-testing 找出哪個視圖被觸摸:以 keyWindow 為起點、hit-test view 為終點,逐級呼叫 hitTest:withEvent:。

hitTest:withEvent: 的處理流程:

先呼叫 pointInside:withEvent: 判斷觸摸點是否在當前視圖內。
— 回傳 YES:遞迴呼叫所有子視圖的 hitTest:withEvent:,順序由層級高到低(top→bottom)。
— 回傳 NO:回傳 nil,該視圖所有子視圖分支全部忽略。
• 若某視圖 pointInside: 回傳 YES 且所有子視圖都回傳 nil(或無子視圖),則該視圖回傳自己。
• 若子視圖回傳非空物件,當前視圖也回傳該物件,沿原路回退,最終把 hit-test view 交給 keyWindow。
• 以下視圖會回傳 nil、無法響應觸摸:hidden=YES、userInteractionEnabled=NO、alpha<0.01、超出父視圖區域的視圖。

2. 觸摸事件的傳遞是從父到子(樹根向葉子遞歸)。若父控件無法接收觸摸,子控件也不可能接收到。

3. 事件的傳遞由上而下(父到子),事件的響應由下而上(子到父)。

4. 對於一個 View,hitTest 為何會呼叫兩次?因為一次是消息傳遞、一次是響應,分別是入棧與出棧操作。

5. 無論視圖能否處理事件,只要點擊就會產生事件,關鍵在於由誰來處理。

6. 父控件的透明度或 hidden 會直接影響子控件:父控件 alpha 為 0 或 hidden=YES,子控件同樣不可見。

7. 技巧:想讓誰成為最適合的 view,就重寫它父控件的 hitTest:withEvent: 回傳指定子控件,或重寫自己的 hitTest:withEvent: 回傳 self。但更建議在父控件中回傳子控件。

8. return nil 的含義:當前 view 與其子控件都不是合適的 view,若兄弟控件也沒有合適者,則父控件成為最適合的 view。

9. [self.view convertPoint:point fromView:self] 可把座標系轉換,再手動呼叫 pointInside 判斷是否落在某 view 內。

【重寫父視圖】

重寫父視圖來控制子視圖的事件響應,是我們更推薦的方式。

(1)RewriteSuperView 繼承自 UIView,重寫 hitTest:

#import 
@interface RewriteSuperView : UIView
@property (nonatomic, strong) UIView *bottom;
@end
#import "RewriteSuperView.h"
@implementation RewriteSuperView
- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event {
    NSLog(@"tag=====%ld",self.tag);
    CGPoint bottomPoint = [self.bottom convertPoint:point fromView:self];
    if ([self.bottom pointInside:bottomPoint withEvent:event]) {
        return self.bottom;
    }
    return [super hitTest:point withEvent:event];
}
@end

意思是:檢測點擊是否落在 bottom 區域,若是則直接用 bottom 響應,否則呼叫父類 hitTest 走正常流程。

(2)VC 實作:

#import "RewriteSuperViewController.h"
#import "RewriteSuperView.h"
#import "Masonry.h"
@interface RewriteSuperViewController ()
@end
@implementation RewriteSuperViewController
- (void)viewDidLoad {
    [super viewDidLoad];
    RewriteSuperView *containerView = [[RewriteSuperView alloc] initWithFrame:CGRectMake(0, 0, 320, 568)];
    containerView.backgroundColor = [UIColor colorWithWhite:0.667 alpha:1.000];
    containerView.tag = 100;
    [self.view addSubview:containerView];
    [containerView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self.view);
    }];
    UIView *bottomView = [[UIView alloc] initWithFrame:CGRectMake(100, 100, 200, 200)];
    bottomView.backgroundColor = [UIColor redColor];
    bottomView.tag = 101;
    [containerView addSubview:bottomView];
    [bottomView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.center.equalTo(containerView);
        make.width.equalTo(@100);
        make.height.equalTo(@100);
    }];
    UITapGestureRecognizer *bottomTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapBottomView:)];
    [bottomView addGestureRecognizer:bottomTap];
    UIView *topView = [[UIView alloc] initWithFrame:CGRectMake(100, 100, 100, 100)];
    topView.backgroundColor = [UIColor yellowColor];
    topView.tag = 102;
    [containerView addSubview:topView];
    [topView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.center.equalTo(containerView);
        make.height.equalTo(@200);
        make.width.equalTo(@200);
    }];
    UITapGestureRecognizer *topTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapTopView:)];
    [topView addGestureRecognizer:topTap];
    containerView.bottom = bottomView;
}
- (void)tapBottomView:(id)sender { NSLog(@"%s",__FUNCTION__); }
- (void)tapTopView:(id)sender { NSLog(@"%s",__FUNCTION__); }
@end

這裡只有灰色 containerView 是 RewriteSuperView,並傳入紅色 bottomView 的引用,讓 hitTest 能進行檢測。

(3)實現後的效果:

灰色是父視圖,黃色是頂部視圖,紅色在底部,黃與紅都是灰色的子視圖且黃覆蓋紅。透過視圖層次調試:

結果是:在黃色區域點擊,若落在紅色區域則由紅色手勢響應;若在紅色之外、黃色之內,則由黃色手勢響應。

下面這張是 Apple 官方文件中的示例圖:

官方說明:觸摸落在 view A 內,於是檢查子視圖 B 與 C;觸摸不在 B 內但在 C 內,於是檢查 D 與 E;觸摸不在 D 內但在 E 內,E 是層級中最低且包含觸摸的視圖,於是成為 hit-test view。建議大家結合 UIButton、UIImageView、UIView 等寫一些案例,會對響應鏈有更深刻的理解。

  1. 上一篇文章:
  2. 下一篇文章:
蘋果刷機越獄教程| IOS教程問題解答| IOS技巧綜合| IOS7技巧| IOS8教程
Copyright © IOS教程網 All Rights Reserved