2011-04-18 64 views
0

我想知道如何將UIView的點擊傳遞給UITextView。這是到目前爲止我的代碼:通過點擊到UITextView

- (void)foundTap:(UITapGestureRecognizer *)recognizer { 

label.text = @"Touch detected"; 

[self.view bringSubviewToFront:aTextView]; 

[aTextView touchesBegan:touches withEvent:event]; 

}

現在,這顯然不爲觸摸和事件的工作沒有定義。但是,我如何定義它們呢?我無法將觸摸聲明爲1(不起作用)。我可以這樣初始化它:

UITouch *touches =[touches anyObject]; 

但是接下來,觸摸仍然未申報。而且我不知道如何宣佈事件。如果您使用- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {}方法,這通常很容易,但我想通過輕敲而不是觸摸。任何幫助將非常感激。


編輯:

我重寫了方法,但我現在還不能通上自來水的UITextView的。我現在需要雙擊才能編輯它,也就是第一次將aTextView放到前面,第二次點擊會編輯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) { 

     label.text = @"Touch"; [self performSelector:@selector(eraseText) withObject:nil afterDelay:2]; 

     [self.view bringSubviewToFront:aTextView]; 
     [self.view bringSubviewToFront:doneEdit]; 

     [aTextView touchesBegan:touches withEvent:event]; 

    } 


} 

回答

0

看看這個以前的SO問題iPhone: Detecting Tap in MKMapView可以幫助你。

+0

嗯...不是真的,除非它表明它通常很難實現。我認爲這是一個非常基本的問題,沒有意識到它是如此的有問題。感謝張貼的鏈接! – 2011-04-18 12:38:32

+0

通過這個http://stackoverflow.com/questions/4446831/getting-the-uitouch-objects-for-a-uigesturerecognizer也 – visakh7 2011-04-18 12:42:46

+0

謝謝,但我不認爲這就是我正在尋找(至少我看不出它與我的問題有什麼關係,但這可能是由於我沒有正確理解問題)。 – 2011-04-18 13:24:13