UIButton 不会触发 touchUpInside 上的目标,除非我将其拖动到外面?

发布于 2025-01-02 06:31:15 字数 2107 浏览 1 评论 0原文

我有一个按钮,当我按下它时,它不会触发我添加的目标选择器。我已将按钮制作为不同的图像,因此我可以看到我正在按下它。

所以这里很有趣,如果我按下并拖动到按钮之外,但不是按钮的直接超级视图之外,目标选择器就会被触发!

我还在测试中在所有超级/子视图上设置了 setClipsToBounds:YES 只是为了确保它仍然在视图边界内。看来是在界限之内。按钮区域外的拖动似乎是全方位的,所以它不像我只能向右点击/拖动。左上右下也工作。我可以点击、拖出然后再放回,这样就可以了。如果我不开始拖动而只是点击并按住,按钮会突出显示,然后返回到未选择状态。

这是按钮的代码。 Button 与 UITextView 一起位于 UIView 上,实际上位于 UITextView 的顶部。所有这些都位于的 UIView 位于较大的视图上,该视图位于缩放/滚动视图上

        messageLookupButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 21, 20)];
        [messageLookupButton setTitle:@"junk" forState:UIControlStateNormal];
        [messageLookupButton sizeToFit];
        [messageLookupButton setTag:kGearButtonView];

            //Get the images for the buttons
        UIImage *gearButtonImage = [UIImage imageNamed:@"gear.png"];
        UIImage *gearButtonImage2 = [UIImage imageNamed:@"StatusLightOn.png"];

        [messageLookupButton setImage:gearButtonImage forState:UIControlStateNormal];
        [messageLookupButton setImage:gearButtonImage2 forState:UIControlStateHighlighted];
        [messageLookupButton addTarget:self action:@selector(gearPressed:) forControlEvents:UIControlEventTouchUpInside];

        [[NSNotificationCenter defaultCenter] addObserver:self
                                                 selector:@selector(processLookupResults:)
                                                     name:kRefreshFromPopover object:nil];
        [self addSubview: messageLookupButton ];
        [messageLookupButton release];

        [messageLookupButton setCenter:CGPointMake(CGRectGetMaxX(elementViewContents.bounds)  -kElementContentFrameOffset  -(CGRectGetWidth( messageLookupButton.bounds)/2), CGRectGetMidY(elementViewContents.bounds))];


        [self bringSubviewToFront:messageLookupButton];

。滚动视图上有几个手势识别器。尽管它们似乎不会干扰我在屏幕上放置的其他按钮和控件。虽然我有一种感觉,滚动视图才是问题所在。

滚动视图代码片段:

[scrollContentView setUserInteractionEnabled:YES];
[scrollContentView setExclusiveTouch:YES];
[scrollContentView setCanCancelContentTouches:YES];
[scrollContentView setDelaysContentTouches:YES];

I have a Button that when I press it, it does not fire the target selector that I've added. I've made the button Different Images, so I can see that I am Pressing it.

So here is the Funny if I Press, and drag outside of the button, but not outside of the button's immediate superView, the target selector is fired!

I've also in testing set the setClipsToBounds:YES on all the super/sub views just to be sure it was still in the views bounds. Seems to be within bounds. The dragging outside the button area seems to be omni directional, so its not like I can only tap/drag right. Left up and down work too. I can tap, drag out then back in and it works. If I don't start to drag and just tap and hold, the button highlights and then goes back to unselected state.

Here is the code for the Button. The Button is on a UIView along with a UITextView, Actually onTop Of the UITextView. The UIView that all of this is on is on a Larger View, which is on a Scaling/Scrolling view

        messageLookupButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 21, 20)];
        [messageLookupButton setTitle:@"junk" forState:UIControlStateNormal];
        [messageLookupButton sizeToFit];
        [messageLookupButton setTag:kGearButtonView];

            //Get the images for the buttons
        UIImage *gearButtonImage = [UIImage imageNamed:@"gear.png"];
        UIImage *gearButtonImage2 = [UIImage imageNamed:@"StatusLightOn.png"];

        [messageLookupButton setImage:gearButtonImage forState:UIControlStateNormal];
        [messageLookupButton setImage:gearButtonImage2 forState:UIControlStateHighlighted];
        [messageLookupButton addTarget:self action:@selector(gearPressed:) forControlEvents:UIControlEventTouchUpInside];

        [[NSNotificationCenter defaultCenter] addObserver:self
                                                 selector:@selector(processLookupResults:)
                                                     name:kRefreshFromPopover object:nil];
        [self addSubview: messageLookupButton ];
        [messageLookupButton release];

        [messageLookupButton setCenter:CGPointMake(CGRectGetMaxX(elementViewContents.bounds)  -kElementContentFrameOffset  -(CGRectGetWidth( messageLookupButton.bounds)/2), CGRectGetMidY(elementViewContents.bounds))];


        [self bringSubviewToFront:messageLookupButton];

The Scroll View has several gesture recognizers on it. Though they don't seem to interfere with other buttons and controls that I've put on the screen. Though I have a feeling its the Scroll View that is the problem.

scroll view code clip:

[scrollContentView setUserInteractionEnabled:YES];
[scrollContentView setExclusiveTouch:YES];
[scrollContentView setCanCancelContentTouches:YES];
[scrollContentView setDelaysContentTouches:YES];

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

深府石板幽径 2025-01-09 06:31:15

我最终只是将齿轮作为图像添加到视图中,然后创建了一个与放置齿轮图标的 UIView 大小相同的不可见按钮。

-(void) addMessageLookupSelector {
        // Set a default Color if there is none
    if (_isMessageLookupField ){
            // Add Gear to End Of Text Field
        if (!messageLookupImageView) {
            messageLookupImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 21, 20)];
            [messageLookupImageView setTag:kGearButtonView];
            [messageLookupImageView setImage:[UIImage imageNamed:@"gear.png"] ];

            [self addSubview: messageLookupImageView ];
            [messageLookupImageView release];
        }
        [self positionMessageLookupImageView ];      
    }
    [self setNeedsDisplay];
}



    - (void)makeControlEditable {

            //Setup the TextField so we can edit its value, need to see if there is a Write Animation Assocated with Element
        [self setTheWriteTag];
        if (writeTag) {
            writeInputRequestedButton = [UIButton buttonWithType:UIButtonTypeCustom];
            [writeInputRequestedButton setFrame:elementViewContents.frame];
            [writeInputRequestedButton setUserInteractionEnabled:YES];
            [writeInputRequestedButton setEnabled:YES];
                // [writeInputRequestedButton setBackgroundColor:[UIColor clearColor]];
            [self addSubview: writeInputRequestedButton];
            [self bringSubviewToFront:writeInputRequestedButton];
            [writeInputRequestedButton addTarget:self action:@selector(wantsToEditValue:) forControlEvents:UIControlEventTouchUpInside];
        }
    }

I ended up Just adding the gear as an Image to the View, then created a invisible button that was the same size as the UIView that I placed the gear icon on.

-(void) addMessageLookupSelector {
        // Set a default Color if there is none
    if (_isMessageLookupField ){
            // Add Gear to End Of Text Field
        if (!messageLookupImageView) {
            messageLookupImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 21, 20)];
            [messageLookupImageView setTag:kGearButtonView];
            [messageLookupImageView setImage:[UIImage imageNamed:@"gear.png"] ];

            [self addSubview: messageLookupImageView ];
            [messageLookupImageView release];
        }
        [self positionMessageLookupImageView ];      
    }
    [self setNeedsDisplay];
}



    - (void)makeControlEditable {

            //Setup the TextField so we can edit its value, need to see if there is a Write Animation Assocated with Element
        [self setTheWriteTag];
        if (writeTag) {
            writeInputRequestedButton = [UIButton buttonWithType:UIButtonTypeCustom];
            [writeInputRequestedButton setFrame:elementViewContents.frame];
            [writeInputRequestedButton setUserInteractionEnabled:YES];
            [writeInputRequestedButton setEnabled:YES];
                // [writeInputRequestedButton setBackgroundColor:[UIColor clearColor]];
            [self addSubview: writeInputRequestedButton];
            [self bringSubviewToFront:writeInputRequestedButton];
            [writeInputRequestedButton addTarget:self action:@selector(wantsToEditValue:) forControlEvents:UIControlEventTouchUpInside];
        }
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文