我的自定义UIView没有收到触摸 [英] My custom UIView dos not receive touches

查看:59
本文介绍了我的自定义UIView没有收到触摸的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用IB,我创建了一个包含几个按钮的小子视图.我的控制器从xib文件读取小视图,并将其作为子视图添加到主视图.加载正常,我可以看到子视图及其所有按钮.据我所知,它也确实连接到控制器中的IBOutlets和IBAction.

Using IB, I have created a small subview that contains a few buttons. My controller reads the small view from the xib-file and adds it as a subview to the main view. It loads okay, I can see the subview and all its buttons. As far as I can see, It does also connect to the IBOutlets and IBActions in the controller.

但是,当我按下按钮时,什么也没发生!实际上,当我按下按钮时,就会调用超级视图的touchesBegan!! 超级视图还包含一个普通按钮"(即我的子视图的同级按钮),并且该按钮可以正常工作!问题:为什么子视图上的按钮不起作用?

However, when I press a button nothing happens! In fact, when I press a button the super view's touchesBegan is invoked! The super view does also contain an "ordinary button" (i.e. a sibling to my subview) and that button works okay! Problem: Why does not the buttons on my subview work?

-(void) loadMultibutt{
    self.buttErase = nil; // test if the outlet connects
    NSArray *arr = [[NSBundle mainBundle]
                    loadNibNamed:@"multibutt_ipad" owner:self options:nil];

    UIView *viewButts = [arr objectAtIndex:0];
    ViewWorkbench* vw = (ViewWorkbench*) self.view;
    [vw addMultibuttView:viewButts];

    // now, buttErase != nil.
    NSSet *setTest = [self.buttErase allTargets];
    NSLog(@"setTest = %@", setTest); // This works!
}

推荐答案

根据您的答复,我可以很确定地说,您的问题是按钮的父视图在其交互区域中不包含按钮.您仍然可以看到它们,因为您没有裁剪子视图,但是仍然无法与它们交互.有两种方法可以解决您的问题

Based on your reply I feel safe concluding that your problem is that the parent view of your buttons does not contain the buttons in its interactive area. You can still see them because you are not clipping the subviews, but still you can't interact with them. There are two options to solve your problem

a)(推荐)将按钮放在适当的位置,以使它们在可见的位置(父视图的交互式区域)

a) (Recommended)Place the buttons in the proper place so that they are within the visible (and interactive area of the parent view)

b)覆盖父视图的hitTest选择器,以便它跟踪其子视图中的所有触摸,包括其边界之外的所有触摸.为此,您将必须创建UIView的子类并实现以下内容:

b) Override the hitTest selector of the parent view, so that it tracks all of touches in its subviews, including those outside of its bounds. For that you will have to create a subclass of UIView and implement the following:

-(UIView *) hitTest:(CGPoint)point withEvent:(UIEvent *)event{
UIView* result = [super hitTest:point withEvent:event];
if (result)
    return result;
for (UIView* sub in [self.subviews reverseObjectEnumerator]) {
    CGPoint pt = [self convertPoint:point toView:sub];
    result = [sub hitTest:pt withEvent:event];
    if (result)
        return result;
}
return nil;

}

这篇关于我的自定义UIView没有收到触摸的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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