2011-05-27 21 views
0

Hola傢伙。 我遇到觸摸處理和UIResponder有點問題。更改FirstResponder

我的目標是管理單點觸摸(移動)以與不同視圖進行交互。

我向我的UIImageView添加了一個手勢識別器,它向我的customViewController發送一條簡單消息。它分配並顯示一個自定義視圖(UIButton的子類)通過點擊的UIImageView。

我將能夠(不必釋放初始手指點按)將觸摸移動發送到我新分配的自定義按鈕。

看來,我的UIImageView吞下我的觸摸,所以我的新Button不能感覺到手指的移動。 我試圖辭去急救員,但似乎沒有效果。

我的自定義按鈕工作很好,但只有當我釋放第一個水龍頭,然後再次點擊新的顯示器按鈕。

有什麼建議嗎? 預先感謝您。

一些代碼在這裏:

裏面我的viewController我連着longPressGestureRecognizer我profileImageView是繪製圍繞profileImageView一個圓形按鈕。

-(void)showMenu:(UIGestureRecognizer*)gesture { 
    [profileImageView removeGestureRecognizer:gesture]; 
    [profileImageView setUserInteractionEnabled:NO]; 

    ConcentricRadius radius; 
    radius.externalRadius = 75; 
    radius.internalRadius = 45; 

    GTCircularButton *circularMenu = [[[GTCircularButton alloc] initWithImage:[UIImage imageNamed:@"radio.png"] highlightedImage:[UIImage imageNamed:@"radio_selected.png"] radius:radius] autorelease]; 
    [circularMenu setTag:kCircularTagControllerCircularMenu]; 
    [circularMenu setCenter:[profileImageView center]]; 

    // Buttons' frames will be set up by the circularMenu 
    UIButton *button1 = [[[UIButton alloc] init] autorelease]; 
    UIButton *button2 = [[[UIButton alloc] init] autorelease]; 
    UIButton *button3 = [[[UIButton alloc] init] autorelease]; 
    UIButton *button4 = [[[UIButton alloc] init] autorelease]; 

    [circularMenu setButtons:[NSArray arrayWithObjects:button1, button2, button3, button4, nil]]; 

    //[[self view] addSubview:circularMenu]; 
    [[self view] insertSubview:circularMenu belowSubview:profileImageView]; 
} 

現在我使用touchMoved方法手動處理circularMenu內的touchEvent。

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event { 
    CGPoint touchLocation = [(UITouch*)[touches anyObject] locationInView:self]; 

    for (UIView *aSubview in _roundButtons) { 
     if ([aSubview isKindOfClass:[UIButton class]]) { 
      UIButton *aButton = (UIButton*)aSubview; 
      CGPoint buttonTouchPointConverted = [aButton convertPoint:touchLocation toView:aButton]; 

      if (CGRectContainsPoint([[aButton layer] frame], buttonTouchPointConverted)) { 
       [self rotateHighlightImage:aButton]; 
      } 
     } 
    } 
} 

目標始終是處理gestureRecognizer的結尾,並將觸摸事件傳遞到創建的新實例,而不必將手指從屏幕上擡起。

任何想法?

PD:我使用touchMoved手動處理觸摸,如果我使用addTarget:action:forControlEvents:在我的按鈕上(1,2,3和4)檢測touchEvent的按鈕吞下它並使其他按鈕無法感覺觸摸他們。

+0

創建觸摸事件,並在事件隊列中插入? – onnoweb 2011-05-27 14:27:23

+0

我該怎麼做? – Gabriele 2011-05-29 08:51:43

回答

1

好吧,大家好我最終解決了我的問題,我達到了我的目標!

我發現真正的問題。 只需GestureRecognizer句柄觸摸Event,直到它的狀態被設置爲UIGestureRecognizerStateEnded,由於這個原因,GestureRecognizer吞下觸摸事件而沒有機會將觸摸事件發送到響應者鏈的其餘部分。

我再次閱讀UIGestureRecognizer類的引用,我發現該屬性已啓用。

爲了解決我的問題,我在手勢啓動目標方法時做的第一件事是將啓用的手勢設置爲NO。這樣手勢立即釋放觸摸控制和touchMoved可以執行。

希望這可以幫助別人。

上面修改後的代碼:

-(void)showMenu:(UIGestureRecognizer*)gesture { 
    [gesture setEnabled:NO];   // This is the key line of my problem!! 
    [profileImageView removeGestureRecognizer:gesture]; 
    [profileImageView setUserInteractionEnabled:NO]; 

    ConcentricRadius radius; 
    radius.externalRadius = 75; 
    radius.internalRadius = 45; 

    GTCircularButton *circularMenu = [[[GTCircularButton alloc] initWithImage:[UIImage imageNamed:@"radio.png"] highlightedImage:[UIImage imageNamed:@"radio_selected.png"] radius:radius] autorelease]; 
    [circularMenu setTag:kCircularTagControllerCircularMenu]; 
    [circularMenu setCenter:[profileImageView center]]; 

    // Buttons' frames will be set up by the circularMenu 
    UIButton *button1 = [[[UIButton alloc] init] autorelease]; 
    UIButton *button2 = [[[UIButton alloc] init] autorelease]; 
    UIButton *button3 = [[[UIButton alloc] init] autorelease]; 
    UIButton *button4 = [[[UIButton alloc] init] autorelease]; 

    [circularMenu setButtons:[NSArray arrayWithObjects:button1, button2, button3, button4, nil]]; 

    //[[self view] addSubview:circularMenu]; 
    [[self view] insertSubview:circularMenu belowSubview:profileImageView]; 
} 
+0

非常感謝,你爲我節省了幾個小時的工作! – 2011-06-16 13:43:22

0

我想你應該改變視圖上的設置,一旦你創建按鈕。例如。

myImageView.userInteractionEnabled = NO; 

這樣,UIImageView將不再聽取觸摸。

讓我知道它是否有效。

+0

不幸的是,它仍然不起作用 – Gabriele 2011-05-27 16:06:16

+0

你可以發佈你創建的代碼並添加按鈕作爲子視圖嗎? – marzapower 2011-05-27 18:24:43

1

touchesEnded:withEvent:將必須與您的touchesMoved:withEvent:但它必須調用這可能是聯繫在一起的控制事件Touch Up Inside事件的方法。

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event { 
    CGPoint touchLocation = [(UITouch*)[touches anyObject] locationInView:self]; 

    for (UIView *aSubview in _roundButtons) { 
     if ([aSubview isKindOfClass:[UIButton class]]) { 
      UIButton *aButton = (UIButton*)aSubview; 
      CGPoint buttonTouchPointConverted = [aButton convertPoint:touchLocation toView:aButton]; 

      if (CGRectContainsPoint([[aButton layer] frame], buttonTouchPointConverted)) { 
       [aButton sendActionsForControlEvents:UIControlEventTouchUpInside]; 
       return; 
      } 
     } 
    } 
} 

我所做的唯一改變是讓按鈕發送控制事件給我們。

原來的答案

在處理你的姿態,這樣做的功能,

if (gesture.state == UIGestureRecognizerStateEnded) { 
    CGPoint point = [gesture locationInView:button]; 
    if (CGRectContainsPoint(button.bounds, point)) { 
     [button sendActionsForControlEvents:UIControlEventTouchUpInside]; 
    } 
} 

此檢查的手勢的最後的接觸是否是按鈕內,並將觸摸的內心活動到其所有目標。

+0

我無法使用sendActionsForControlEvents,因爲手勢識別器il連接到UIImageView。我會在幾分鐘後發佈代碼。萬分感謝! – Gabriele 2011-05-29 08:50:54

+0

我在一個圖像視圖對象上測試了這個代碼。它的工作,所以我有點驚訝。 – 2011-05-29 08:57:59

+0

sendActionsForControlEvents是UIControl類中的一個方法,所以我不能在UIImageView上使用此方法,因爲它不是UIControl的父類。您可以使用按鈕對象上的sendActionsForControlEvents,該按鈕對象可能是UIButton,它是UIControl。也許我可以更改我的profileImageView到UIButton而不是UIImageView並嘗試你的代碼。 – Gabriele 2011-05-29 09:57:42