ios - 忽略透明 UIControl 上的触摸 - 不由添加的操作函数处理

标签 ios cocoa-touch uiview hittest uicontrol

我有一个包含一些其他控件的自定义 UIControl。在这些控件之间有空白空间,我的 UIControl 的背景需要是透明的。

我需要捕获在我的自定义 UIControl 上发生的所有 触摸事件,即使它们仅发生在其他控件之间(在透明区域上)。我不能使用手势识别器,我需要比它们提供更多的控制。相反,我想像这样注册触摸处理函数:

myControl.addTarget(self, action: "handleTouchDown:event:", forControlEvents: UIControlEvents.TouchDown)

通过这种方法,我收到了 myControl 的非透明区域发生的触摸,但没有发生在透明背景下的触摸。

我尝试在我的自定义控件中覆盖 hitTest::point:withEvent 以不检查 alpha 值。但是 hitTest::point:withEvent 甚至不会在触摸发生在透明控制区域时被调用。我用自定义 CALayer 替换了我的控件的 layer 并且也覆盖了 hitTest 但没有结果(hitTest 在层似乎根本没有被调用)。


更多细节(编辑)

要提供完美答案(并赢得赏金),您需要做的就是:

  1. 创建简单的应用程序,添加一个UIControl(例如UIButton)。
  2. UIControl 中删除所有内容(来自 UIButton 的文本)并使其背景透明(设置为透明颜色或将 alpha channel 设置为 0)。
  3. 使用addTarget::action:forControlEvents: 方法在控件上注册UIControlEvents.TouchDown 事件。在处理程序方法中打印一些内容到控制台。
  4. 运行应用程序,按下控件。没有任何内容打印到控制台。使其工作 - 不要使用手势识别器我需要 addTarget::action:forControlEvents: 提供的粒度。没有黑客解决方案是首选。我知道将控件上的背景 alpha channel 设置为 0.01 会使它突然工作,但这是我不想要的一种 hack。在这里描述你做了什么。

最佳答案

在您的编辑部分之后:

https://github.com/soxjke/TransparentControl

1) 如果我将背景色设置为 +[UIColor clearColor] 效果很好。所以你不需要做更多的事情,用清晰的颜色继续吧。 (顶部按钮)
2) 如果我设置 alpha = 0,则不处理触摸。确定(中间按钮)
3) 要处理这种触摸,有一个简单的解决方案(底部按钮),子类 UIButton(实际上您可以使用层次结构中的任何东西,直到 UIView)。覆盖 hitTest:withEvent:

- (UIView*)hitTest:(CGPoint)point withEvent:(UIEvent *)event
{
    return CGRectContainsPoint(self.bounds, point) ? self : nil;
}

利润
4)如果你需要更深入,使用

touchesBegan:withEvent:
touchesMoved:withEvent:
touchesEnded:withEvent:
touchesCancelled:withEvent:

在你的 UIResponder 子类上,如 Rob Glassey在他的 answer 中提出

附言我将以题外话结束。不知道你的任务到底是什么,但是告诉你不能使用识别器因为你需要对所有触摸事件进行“更多控制”会发现你不知道手势识别器的可能性。因此,根据我的经验,我会说你更愿意发明自行车,而不是提供好的解决方案。

附言如果我和这里的其他人提出的方法对你不起作用 - 检查你的控件的 userInteractionEnabled

附录(查看要测试的 Controller 代码:):

#import "ViewController.h"
#import "TransparentControl.h"

@interface ViewController ()

@property (weak, nonatomic) IBOutlet UIButton *buttonClearColor;
@property (weak, nonatomic) IBOutlet UIButton *buttonAlpha0;
@property (weak, nonatomic) IBOutlet TransparentControl *customButtonAlpha0;

@end

@implementation ViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    [self.buttonClearColor addTarget:self action:@selector(touchUpInside:) forControlEvents:UIControlEventTouchUpInside];
    [self.buttonClearColor addTarget:self action:@selector(touchUpOutside:) forControlEvents:UIControlEventTouchUpOutside];
    [self.buttonClearColor addTarget:self action:@selector(touchDown:) forControlEvents:UIControlEventTouchDown];

    [self.buttonAlpha0 addTarget:self action:@selector(touchUpInside:) forControlEvents:UIControlEventTouchUpInside];
    [self.buttonAlpha0 addTarget:self action:@selector(touchUpOutside:) forControlEvents:UIControlEventTouchUpOutside];
    [self.buttonAlpha0 addTarget:self action:@selector(touchDown:) forControlEvents:UIControlEventTouchDown];

    [self.customButtonAlpha0 addTarget:self action:@selector(touchUpInside:) forControlEvents:UIControlEventTouchUpInside];
    [self.customButtonAlpha0 addTarget:self action:@selector(touchUpOutside:) forControlEvents:UIControlEventTouchUpOutside];
    [self.customButtonAlpha0 addTarget:self action:@selector(touchDown:) forControlEvents:UIControlEventTouchDown];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
}

- (void)touchUpInside:(id)sender
{
    NSLog(@"%s", __PRETTY_FUNCTION__);
}

- (void)touchDown:(id)sender
{
    NSLog(@"%s", __PRETTY_FUNCTION__);
}

- (void)touchUpOutside:(id)sender
{
    NSLog(@"%s", __PRETTY_FUNCTION__);
}

@end

关于ios - 忽略透明 UIControl 上的触摸 - 不由添加的操作函数处理,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26644820/

相关文章:

ios - 使用 Storyboard 在屏幕上显示两个 View Controller ,例如 SplitViewController

php - 像 objective c block 一样在 php 中阻塞?

iphone - _NSZombie_GraphicPath 类在两者中都实现了 ??和 ??。将使用两者之一。哪个是未定义的

iphone - UITableViewCell 上的 selectedBackgroundView 应用于单元格中的所有 View

ios - 单击 UIWebview 上的链接时打开 UIView

ios - addSubView - 在 UIView 中添加一个按钮也会为其 SuperView 添加相同的按钮

ios - 如何处理多次写入,以便在一次写入失败时不提交数据?

ios - 如何在 iOS 中 UITableViewCell 的右侧放置另一个 TextLabel

iphone - iOS 根据设备方向切换 View Controller

ios - 使用过渡动画 ios4.0+ 重新加载/刷新 ViewController 的 SuperView