2011-04-19 4 views
1

申し訳ありませんが、非常に基本的な初心者の質問です。UITextFieldとUITextViewがあるとします。次に、これらのTextField/Viewの上に透明なUIViewが配置されていることを想像してください。タッチすると応答しません。cocoa-touch:どのオブジェクトに触れたか調べる方法

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がタッチされた場合にのみ、firstresponderを取り除くれます:

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

} 

答えて

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 ... 
} 
4
UITouch *touch = [touches anyObject]; 

if (touch.view == mybutton){ 

     NSLog(@"My Button pressed"); 
} 
+0

感謝します。私は実際に関連する質問を投稿しました:http://stackoverflow.com/questions/7597909/defining-maximum-variance-of-an-uitextview –

1

注:この場合MYVIEWがuserInteractionを有効にする必要がありますが(myView.userInteractionEnabled = YES)、そうでない場合、touchイベントはuserInteractionが有効なスーパービューに渡されます。

UITouch * touch = [touches anyObject];

(touch.view == MYVIEW)あなたの思考のための{

NSLog(@"My myView touched"); 

}

関連する問題