1

申し訳ありませんが、非常に基本的な初心者の質問: UITextField と UITextView があると想像してください。次に、これらの TextField/View の上に配置された透明な UIView を想像してください。これにより、それらに触れても応答しなくなります。

(スワイプやピンチなどではなく)UIViewに触れると、ユーザーが触れたもの(TextFieldやViewなど)が最初の応答者になることを望みます。次のようなコマンドはありますか:

[objectThatWasTouched becomeFirstResponder];?

これは、次の方法で必要になります。今はUITextViewがファーストレスポンダーになるように設定していますが、触れたものは何でもファーストレスポンダーにしたいです。

 - (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event{

    [super touchesMoved:touches withEvent:event];

    UITouch *touch = [touches anyObject];
    CGPoint currentPosition = [touch locationInView:self.view];

    CGFloat deltaX = fabsf(gestureStartPoint.x - currentPosition.x); // will always be positive
    CGFloat deltaY = fabsf(gestureStartPoint.y - currentPosition.y); // will always be positive


    if (deltaY == 0 && deltaX == 0) {

        [self.view bringSubviewToFront:aSwipeTextView];
        [self.view bringSubviewToFront:noteTitle];
        [self.view bringSubviewToFront:doneEdit];


        [aSwipeTextView becomeFirstResponder];


    }


}

そして最後に、ファーストレスポンダーを取り除くために何らかの方法が必要になります。つまり、ファーストレスポンダーが何であるかに依存します。UITextView がタッチされた場合、これは最初のレスポンダーのみを取り除きます。

    - (IBAction)hideKeyboard
{
    [aSwipeTextView resignFirstResponder];
    [self.view bringSubviewToFront:canTouchMe];

}
4

3 に答える 3

4
UITouch *touch = [touches anyObject];

if (touch.view == mybutton){

        NSLog(@"My Button pressed");
}
于 2011-10-01T09:33:17.387 に答える
2

次のようなことを試すことができます:

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event{

    [super touchesMoved:touches withEvent:event];

    UITouch *touch = [touches anyObject];
    CGPoint currentPosition = [touch locationInView:self.view];

    UIView *hitView = [self hitTest:currentPosition withEvent:event];

    if([hitView isKindOfClass:[UIResponder class]] && [hitView canBecomeFirstResponder]) {
       [hitView becomeFirstResponder];
    }

    // ... more code ...
}
于 2011-04-19T22:15:29.210 に答える
1

注: この場合、myView は userInteraction を有効にする必要があります (myView.userInteractionEnabled = YES)。それ以外の場合、タッチ イベントは、userInteraction が有効になっている superView に渡されます。

UITouch *touch = [任意のオブジェクトに触れる];

if (touch.view == myView){

    NSLog(@"My myView touched");

}

于 2014-05-21T09:27:25.777 に答える