響應者鏈相關問題

場景:

開發中事件的傳遞(響應者鏈)相關問題是不可避免的,本文是作者在開發中所遇到的問題和解決方案的集合,希望對每個讀者有用
1、 在使用MVC架構模式中,不可避免的使view和controller分離于是我們不可避免的使用在視圖中找控制器的操作,貼一段一直在用的代碼:

//得到此view 所在的viewController
- (UIViewController*)viewController{
    for (UIView* next = [self superview]; next; next = next.superview) {
        UIResponder* nextResponder = [next nextResponder];
        if ([nextResponder isKindOfClass:[UIViewController class]]) {
            return (UIViewController*)nextResponder;
        }
    }
    return nil;
}

2、 在ScrollView使用touchBegin方法是由于UIView的Touch方法被ScrollView攔截了,其解決方案如下:
創建UIScrollView的子類在子類中重寫方法,保證事件的向下傳遞 。閑話不多說見代碼

  #import <UIKit/UIKit.h>

  @interface UIScrollView (HAScrollView)
    - (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event;
    - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event;
    - (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event;
  @end

實現方法

#import "UIScrollView+HAScrollView.h"

@implementation UIScrollView (HAScrollView)
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    [[self nextResponder] touchesBegan:touches withEvent:event];
    [super touchesBegan:touches withEvent:event];
}
-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
    [[self nextResponder] touchesMoved:touches withEvent:event];
    [super touchesMoved:touches withEvent:event];
}

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
    [[self nextResponder] touchesEnded:touches withEvent:event];
    [super touchesEnded:touches withEvent:event];
}

在和在輸入框中進行手寫輸入操作時候會出現閃退的問題,其原因是上述問題(原因沒找到),可以通過jiejue

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容

  • Android 自定義View的各種姿勢1 Activity的顯示之ViewRootImpl詳解 Activity...
    passiontim閱讀 173,373評論 25 708
  • Spring Cloud為開發人員提供了快速構建分布式系統中一些常見模式的工具(例如配置管理,服務發現,斷路器,智...
    卡卡羅2017閱讀 134,948評論 18 139
  • 發現 關注 消息 iOS 第三方庫、插件、知名博客總結 作者大灰狼的小綿羊哥哥關注 2017.06.26 09:4...
    肇東周閱讀 12,245評論 4 61
  • 轉眼二寶5個月了。去年這個時候,她才在我肚子里安營扎寨,如今,已活脫脫的牢牢握在掌心里了,我歡喜自己又一次迎接新生...
    嘉卉1閱讀 703評論 0 2
  • 黑夜是自卑者的溫柔鄉是作惡者的狂歡節 黑夜躲藏在黑夜掩飾身份,樹立假墓碑掩飾表情,鍛造新面具掩飾語言,反芻舊詞語 ...
    河島閱讀 315評論 14 13