2011-04-19 43 views
1

對不起,但一個非常基本的初學者的問題:想象一下有一個UITextField和一個UITextView。然後,想象一下放置在這些TextField/View上方的透明UIView,如果您觸摸它們,它們將不會響應。如果你觸摸(而不是刷卡或捏等)UIView,我希望無論用戶觸摸(即TextField或視圖)成爲第一響應者。有沒有類似的命令:cocoa-touch:如何找出哪個對象被觸摸

[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]; 


    } 


} 

最後,我需要某種方法來擺脫第一響應者,即取決於任何第一響應者。這將只得到擺脫firstresponder的,當UITextView的已經感動:

- (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 – 2011-10-01 13:18:55

1

注:在這種情況下MyView的應該是啓用的用戶交互(myView.userInteractionEnabled = YES),否則觸摸事件將被傳遞給啓用了userInteraction的superView。

UITouch * touch = [touches anyObject];

如果(touch.view == MyView的){

NSLog(@"My myView touched"); 

}

相關問題