简体   繁体   English

iOS操作:将UIButton添加到另一个UIButton时出现@selector问题。 动作仅适用于按钮相交的位置

[英]iOS action:@selector issue when UIButton is added to another UIButton. Action only works where buttons intersect

I have a UIButton2 that is added on to UIButton1: 我有一个添加到UIButton1的UIButton2:

按钮相交

The action @selector only gets called where UIButton2 intersects UIButton1 (the "b"). 仅在UIButton2与UIButton1(“b”)相交时才调用@selector动作。 So, a tap in the "a" or "c" area, the action method (addDockButtonTouchedDown) does NOT get called, but it does when you tap in the "b" section. 因此,在“a”或“c”区域中点击,操作方法(addDockButtonTouchedDown)不会被调用,但是当您点击“b”部分时它会被调用。 Here's some code: 这是一些代码:

    // button1 is created elsewhere in the code and is a subclass of UIButton for UI layout (has a footer label and title label set). nothing fancy going on.  button1 userInteractionEnabled is set to yes 
    // button2 is below... 
    NSString *dockPath = [[NSBundle mainBundle] pathForResource:@"AddToMyDock" ofType:@"png"];
    UIImage *dockImage = [UIImage imageWithContentsOfFile:dockPath];
    CGRect rect = CGRectMake(-20.0f, -10.0f + dockImage.size.height+1, dockImage.size.width, dockImage.size.height);
    UIButton *button2 = [[UIButton alloc] initWithFrame:rect];
    button2.userInteractionEnabled = YES;
    button2.exclusiveTouch = YES;
    [button2 setBackgroundImage:dockImage forState:UIControlStateNormal];
    [button2 addTarget:self action:@selector(addDockButtonTouchedDown:)
         forControlEvents:UIControlEventTouchDown];
    [button1 addSubview:button2];

Any ideas why this is happening and how to fix it??? 任何想法为什么会发生这种情况以及如何解决它? Please don't suggest to simply make button1 bigger. 请不要建议简单地将button1做大。

First of all, adding a button as a subview of another button isn't really common design and I would avoid doing that. 首先,添加一个按钮作为另一个按钮的子视图并不是真正常见的设计,我会避免这样做。 Simply create a container UIView that holds both buttons. 只需创建一个容纳两个按钮的容器UIView。

The problem you're seeing is because while your button may be displayed beyond the bounds of its superview (the other button), actions on the outside of button1's bounds won't be registered. 您看到的问题是因为当您的按钮可能超出其超级视图的边界(另一个按钮)时,将不会注册button1边界外部的操作。 The only reason you can see button2, is because clipsToBounds is set to NO on button1, which allows its subviews to be drawn outside of its bounds. 您可以看到button2的唯一原因是因为clipsToBounds在button1上设置为NO ,这允许其子视图在其边界之外绘制。

So, the only way to make your current solution work is indeed by making button1 bigger. 因此,使当前解决方案正常工作的唯一方法就是使button1更大。 However, the better option would be to create superview that's big enough to hold both buttons, and add both buttons to it. 但是,更好的选择是创建超级视图,该视图足以容纳两个按钮,并为其添加两个按钮。

It not works on "a" and "c" regions because the hit test for the touch event fails there when called for button1. 它不适用于“a”和“c”区域,因为触摸事件的命中测试在调用button1时失败。 You should subclass UIButton for button1 and override the hit test with something like this: 您应该为button1子类化UIButton并使用以下内容覆盖命中测试:

 - (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event {
    if(CGRectContainsPoint(self.childButton.frame, point)) {
        return [self.childButton hitTest:[self convertPoint:point toView:self.childButton] withEvent:event];
    }
    return [super hitTest:point withEvent:event];
 }

Note: childButton is button2 in your example. 注意:childButton在您的示例中是button2。

Note2: I think that however this should work, the ugliness of this is a sign of a design problem, but that's your business. 注2:我认为不过这应该有效,这是一个设计问题的迹象,但这是你的事。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM