当userInteractionEnabled

当userInteractionEnabled

本文介绍了当userInteractionEnabled = YES时,如何将触摸事件传递给superview?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下设置。

+- XXXCustomControl : UIControl -------+
| A                                    |
|   +- ContentView -------------------+|
|   |                                 ||
|   |  B                              ||
|   |                                 ||
|   +---------------------------------+|
+--------------------------------------+

一个XXXCustomControl,它是UIControl的子类。它包含一个名为contentView类型UIView的子视图,其大小小于控件的区域。
该视图有.userInteractionEnabled = YES;

A XXXCustomControl that is a subclass of UIControl. It contains one subview called contentView of type UIView with size that is smaller than the Control's area..That view has .userInteractionEnabled = YES;

我需要属性设置为YES,因为水平scrollview被放在这里一次,而他们需要可滚动。如果superview(在我们的情况下内容视图不允许用户交互,这是继承的子视图)。
但同时,这个XXXCustomControl需要是tappable当它在其内容视图不包含scrollview不仅在区域A,但也在区域B中。

I need that property to have set to YES, because horizontal scrollviews are put inside this once in a while and they need to be scrollable. If the superview (in our case content view would not allow user interaction, this is inherited y the subviews.)But at the same time this XXXCustomControl need to be tappable when it contains no scrollview in its content view not only in area A but also in area B.

因此我有一个利益冲突,因为我

So I have a "conflict of interests" here because I either

1)将内容视图设置为userInteractionEnabled = NO,然后我可以在A和B中点击内容视图区域中的空控件,但我将放在那里的滚动视图不会滚动。

1) set the content view to userInteractionEnabled = NO, then I can tap the empty control in the content view area both in A and B, but the scrollviews I will put there won't be scrollable..

2)将内容视图设置为userInteractionEnabled = YES,但是如果Control为空,我只能点按区域A触发触摸事件。

2) set the content view to userInteractionEnabled = YES but then, if the Control s empty, I can only tap area A to trigger a touch event.

我想出的一个想法是,我将属性设置为NO默认情况下,当我填充contentView我设置为yes。当我清除contentView我设置属性回到否。
基本上我想这个设置为yes所有的时间,当它是空的,强制contentView传递touchUpInside事件到其superview。

One idea I came up with is that I set the property to NO by default and when I populate the contentView I set it to yes. when I clear the contentView I set the property back to no.Basically I want this to have set to yes all the time, and when it is empty ,force the contentView to pass the touchUpInside event up to its superview.

这是可能吗?

推荐答案

您可以尝试覆写pointInside:withEvent:方法。这将允许您在转发触摸到超级视图时返回NO:

You could try overriding the pointInside:withEvent: method in your inner view. This will allow you to return NO when you want to forward touches to the superview:

-(BOOL)pointInside:(CGPoint)point withEvent:(UIEvent *)event
{
    if( /* You have content, and you want to receive touches */){
        return YES;
    }else{
        return NO;
    }
}

这篇关于当userInteractionEnabled = YES时,如何将触摸事件传递给superview?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-29 21:58