UIScrollView 上如果有UITextField的話导梆,結(jié)束編輯(退出鍵盤)直接用touchesBegan方法無(wú)效偎蘸,需要再給UIScrollView加一個(gè)分類峦剔,重寫幾個(gè)方法。
網(wǎng)上已經(jīng)有很多前輩給了相關(guān)代碼是這樣的(閱前提示:這樣是有問(wèn)題的和蚪!):
- (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];
}
這樣會(huì)有一個(gè)嚴(yán)重問(wèn)題,就是使用手寫輸入法輸入中文會(huì)導(dǎo)致崩潰(雖然使用手寫輸入法的人不多,但也不能無(wú)視他們)慷蠕。被坑死,問(wèn)題是百度出來(lái)尼瑪80~90%全是這種解決方法扣泊。坑死人嘶摊!
有一些前輩對(duì)于“UIScrollView點(diǎn)擊空白處退出鍵盤”就提出了另一種解決方法:加一層view延蟹,給view一個(gè)點(diǎn)擊事件,退出鍵盤叶堆。
但是我的項(xiàng)目中已經(jīng)被前一種方法坑了阱飘,已經(jīng)有用戶反映手寫崩潰,換第二種方法的話很麻煩虱颗,需要修改之后重新提交審核沥匈,不能及時(shí)解決,我需要及時(shí)的用JSPatch線上打補(bǔ)丁解決忘渔。調(diào)試了很久高帖,我發(fā)現(xiàn)手寫鍵盤在調(diào)用UIScrollView的這個(gè)分類的方法時(shí),self的類型是UIKBCandidateCollectionView畦粮,一種系統(tǒng)沒(méi)有暴露出來(lái)的類型散址,應(yīng)該是UIScrollView的一個(gè)子類,所以解決辦法就呼之欲出了锈玉,直接上代碼爪飘。
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
if (![self isMemberOfClass:[UIScrollView class]]) {
} else {
[[self nextResponder] touchesBegan:touches withEvent:event];
if ([super respondsToSelector:@selector(touchesBegan:withEvent:)]) {
[super touchesBegan:touches withEvent:event];
}
}
}
-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
if (![self isMemberOfClass:[UIScrollView class]]) {
} else {
[[self nextResponder] touchesMoved:touches withEvent:event];
if ([super respondsToSelector:@selector(touchesBegan:withEvent:)]) {
[super touchesMoved:touches withEvent:event];
}
}
}
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
if (![self isMemberOfClass:[UIScrollView class]]) {
} else {
[[self nextResponder] touchesEnded:touches withEvent:event];
if ([super respondsToSelector:@selector(touchesBegan:withEvent:)]) {
[super touchesEnded:touches withEvent:event];
}
}
}
手寫輸入法崩潰完美解決O(∩_∩)O~~