場景:
開發中事件的傳遞(響應者鏈)相關問題是不可避免的,本文是作者在開發中所遇到的問題和解決方案的集合,希望對每個讀者有用
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