很多时候为了满足需求,我们需要在UIScrollView添加UITextField,在处理键盘升降键盘事件的时候往往会写个UIScrollView的分类,代码
- (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];
}
后来我们的测试发现一个问题,当使用iOS自带手写键盘的时候输入第一个字变灰,再点击点击奔溃了。
调试了很久,我发现手写键盘在调用UIScrollView的这个分类的方法时,self的类型是UIKBCandidateCollectionView,具体是什么还没有去苹果官方文档查,先屏蔽了,代码
- (void)touchesBegan:(NSSet<UITouch *> *)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];
}
}
}
解决
网友评论