超越uiview的界限 [英] interaction beyond bounds of uiview

查看:121
本文介绍了超越uiview的界限的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当UIButton框架位于其父框架之外时,UIButton(或任何其他控件)是否可能接收触摸事件?因为当我尝试这个,我的UIButton似乎不能接收任何事件。我如何解决这个问题?

Is it possible for a UIButton (or any other control for that matter) to receive touch events when the UIButton's frame lies outside of it's parent's frame? Cause when I try this, my UIButton doesn't seem to be able to receive any events. How do I work around this?

推荐答案

是的。您可以覆盖 hitTest:withEvent:方法来返回与该视图包含的点相比更大的一组点的视图。请参阅 UIView类参考

Yes. You can override the hitTest:withEvent: method to return a view for a larger set of points than that view contains. See the UIView Class Reference.

编辑:例如:

- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event
{
    CGFloat radius = 100.0;
    CGRect frame = CGRectMake(-radius, -radius,
                              self.frame.size.width + radius,
                              self.frame.size.height + radius);

    if (CGRectContainsPoint(frame, point)) {
        return self;
    }
    return nil;
}

编辑2:为了确保按钮被视为在父节点的范围内,您需要在父节点中覆盖 pointInside:withEvent:以包含按钮的框架。

Edit 2: (After clarification:) In order to ensure that the button is treated as being within the parent's bounds, you need to override pointInside:withEvent: in the parent to include the button's frame.

- (BOOL)pointInside:(CGPoint)point withEvent:(UIEvent *)event
{
    if (CGRectContainsPoint(self.view.bounds, point) ||
        CGRectContainsPoint(button.view.frame, point))
    {
        return YES;
    }
    return NO;
}

注意不太正确。 As Summon解释如下:

Note the code just there for overriding pointInside is not quite correct. As Summon explains below, do this:

-(BOOL)pointInside:(CGPoint)point withEvent:(UIEvent *)event
    {
    if ( CGRectContainsPoint(self.oversizeButton.frame, point) )
        return YES;

    return [super pointInside:point withEvent:event];
    }

注意,你很可能用 self.oversizeButton 作为此UIView子类中的IBOutlet;那么你可以只是拖动超大按钮的问题,到,有问题的特殊视图。 (或者,如果由于某种原因,你在一个项目中做了很多事情,你会有一个特殊的UIButton子类,你可以通过子视图列表查看这些类。)希望它有帮助。

Note that you'd very likely do it with self.oversizeButton as an IBOutlet in this UIView subclass; then you can just drag the "oversize button" in question, to, the special view in question. (Or, if for some reason you were doing this a lot in a project, you'd have a special UIButton subclass, and you could look through your subview list for those classes.) Hope it helps.

这篇关于超越uiview的界限的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆