iOS 開発: 空白部分をクリックしてキーボードを終了します

1. キーボードを終了するために以前に使用した方法

UIScrollView に UITextField がある場合、touchesBegan メソッドを直接使用して編集を終了する(キーボードを終了する)ことは無効であるため、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];
}

この方法が一般的に使用されますが、この方法で書くと問題があり、システムの手書きキーボードを使用するとクラッシュの問題が発生します。_
キーボード入力を手書きする場合、UIScrollView の - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event; この分類方法、self の型は UIKBCandidateCollectionView であり、システムによって公開されていない型であり、次のサブクラスである必要があります。 UIScrollView があるため、システムがクラッシュします。

2. 判断力を高めて手書き入力のクラッシュを回避する

- (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];
        }
    }
}

この方法でもUIscrollviewでは問題ありませんが、UItableviewでキーボードをキャンセルすると失敗します。

3. クリックジェスチャーでキーボードを終了します

タップ ジェスチャを使用して問題を回避する

UITapGestureRecognizer *tapView = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(touchViewCancelKeyBoard:)];
    tapView.cancelsTouchesInView = NO;
    [self.view addGestureRecognizer:tapView];
- (void)touchViewCancelKeyBoard:(UITapGestureRecognizer *)tap {
    [self.TextField resignFirstResponder];
}

4. スクロールビューに付属のプロパティ

scsrollView.keyboardDismissMode = UIScrollViewKeyboardDismissModeOnDrag;

このうち、keyboardDismissMode は UIScrollView のプロパティで、その値は、ドラッグ時にキーボードが消えることを意味する UIScrollViewKeyboardDismissModeNone と UIScrollViewKeyboardDismissModeOnDrag 、および指を下にスライドさせるとキーボードが画面外に移動できることを意味する UIScrollViewKeyboardDismissModeInteractive に
加えて追加されます。

このコンテンツが皆様のお役に立てば幸いです

おすすめ

転載: blog.csdn.net/guoxulieying/article/details/117438308#comments_18148682