信念和目標(biāo)孕暇,必須永遠洋溢在程序員內(nèi)心。
問題的實例
前兩天在做注冊界面的時候(類似于下圖),由于注冊的條件過多,所以像5s等根本就不放下去,那么解決的方案就出現(xiàn)了兩種,一種就是使用tableView的形式做注冊頁面,另外一種方式就是用UIscrollView做一個滑動視圖,然后上面添加各種輸入框,我選擇的是第二種(看起來麻煩,其實處理起來差不多,這個問題我會在后面的博客做詳解),既然是注冊,那么必不可少的就是輸入框UITextField了,當(dāng)我們輸入完成的時候,我們想要取消第一響應(yīng)者,我們實現(xiàn)了- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event 這個方法,但是程序根本不取消對應(yīng)UITextField的第一響應(yīng)者..
注冊頁面
問題的原因
原因很簡單,UIView的touch事件被UIScrollView捕獲了,UIView獲取不到touch事件,自然不能取現(xiàn)對應(yīng)的UITextField的第一響應(yīng)者了.
問題的解決方案
既然我們知道了UIView的touch事件被UIScrollView捕獲了 ,只要讓讓UIScrollView將事件傳遞過去就可以了坷澡。最簡單的解決辦法就是加一個UIScrollView的category。這樣每個用到UIScrollView的地方只要導(dǎo)入這個category就可以直接響應(yīng)相關(guān)的touch事件了。 我們看一下category(延展)當(dāng)中我們改如何重寫touch相關(guān)的方法.
#import "UIScrollView+UITouch.h"
@implementation UIScrollView (UITouch)
- (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];
}
@end
本博客相關(guān)的Demo沒有做,如果有任何疑問,請在下面評論區(qū)寫上您的問題,我會及時的回復(fù)您.謝謝.