2011-05-27 52 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