English 中文(简体)
更改第一个响应程序
原标题:Change the FirstResponder

Hola guys. I m experiencing a little problem with touch handling and UIResponder.

我的目标是管理一次触摸(移动)来与不同的视图交互。

我在UIImageView中添加了一个手势识别器,它会向我的customViewController发送一条简单的消息。它在点击的UIImageView上分配并显示一个自定义视图(子类化UIButton)。

我可以(不必释放最初的手指敲击)将触摸移动发送到我新分配的自定义按钮。

It seems that my UIImageView swallow my touch and so my new Button can t feel the finger movement. I tried to resign first responder but it seems having no effect.

我的自定义按钮工作得很好,但前提是我释放第一次点击,然后再次点击新的显示按钮。

Any suggests? Thank you in advance.

此处的某些代码:

在我的viewController中,我将一个长按手势识别器连接到我的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];
            }
        }
    }
}

我们的目标始终是处理手势识别器的结束,并将触摸事件传递给创建的新实例,而无需将手指从屏幕上移开。

有什么想法吗?

PD:我用touchMoved手动处理触摸,因为如果我使用addTarget:action:forControlEvents:在我的按钮(1、2、3和4)上,检测到touchEvent的按钮会吞下它,使其他按钮无法感觉到触摸在它们上面移动。

最佳回答

好了,伙计们,我终于解决了我的问题,我达到了我的目标!!

I discovered what was the real problem. Simply the GestureRecognizer handle touch Event until it s state is setted to UIGestureRecognizerStateEnded and for this reason the GestureRecognizer swallow touch events with no opportunity to send touch events to the rest of responder chain.

我再次阅读了UIGestureRecognizer类引用,发现该属性已启用。

为了解决我的问题,当手势启动目标方法时,我要做的第一件事是将手势设置为“否”。通过这种方式,手势可以立即释放触摸控制和触摸移动。

希望这能帮助到别人。

修改后的代码上方:

-(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];
}
问题回答

您的触摸Ended:withEvent:必须与您的触摸Moved:withEvent:[/code>相同,除非它必须调用可能与控制事件Touch-UpInside事件相关的方法。

- (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];
    }
}

这会检查手势的最后一次触摸是否在按钮内部,并向其所有目标发送一个触摸内部事件。

我认为一旦创建了按钮,就应该更改视图的设置。例如。

myImageView.userInteractionEnabled = NO;

这样,UIImageView将不再监听触摸。

如果有效,请告诉我。





相关问题
Code sign Error

I have created a new iPhone application.I have two mach machines. I have created the certificate for running application in iPhone in one mac. Can I use the other mac for running the application in ...

ABPersonViewController Usage for displaying contact

Created a View based Project and added a contact to the AddressBook using ABAddressBookRef,ABRecordRef now i wanted to display the added contact ABPersonViewController is the method but how to use in ...

将音频Clips从Peter改为服务器

我不禁要问,那里是否有任何实例表明从Peit向服务器发送音响。 I m不关心电话或SIP风格的解决办法,只是一个简单的袖珍流程......

• 如何将搜查线重新定位?

我正试图把图像放在搜索条左边。 但是,问题始于这里,搜索条线不能重新布署。