如何在iOS中向superview发送触摸事件? [英] How to send touch events to superview in iOS?

查看:92
本文介绍了如何在iOS中向superview发送触摸事件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在做一个iPad应用程序。如何将触摸事件发送到其超级视图?

I am doing an iPad app. How do I send touch events to its superview?

我正在添加一个始终是其他活动的视图。在那我我将scrollview添加到屏幕的一半,在另一半我添加另一个视图。一切正常,现在我想添加一个按钮和一个小描述视图,当按钮被点击时会出现,再次点击按钮时会再次消失。为了实现这一点,我拍摄了一个覆盖整个视图并使背景颜色清晰的视图。我将此按钮和描述视图放在此视图中,但由于透明视图,它不会滚动。

I am adding a view which is always some other activity. On that I am adding scrollview to half the screen and in the other half I add another view. It all works fine, now I want to add a button and a small description view that will appear when button is tapped and disappear again when button is tapped again. In order to implement this I took a view which covers the entire view and made its back ground color clear. I place this button and description view into this view but it wont scroll because of transparent view.

我想使此透明视图忽略其事件。

I want to make this transparent view ignore its events.

可以这样做吗?

推荐答案

设置视图的 userInteractionEnabled 属性为。这样可以让所有人触摸到它后面的视图。

Set the view's userInteractionEnabled property to NO. This lets all touches through to the view behind it.

您可以在属性检查器的界面构建器/故事板中或代码中设置它:

You can set it in the interface builder/storyboards in the attributes inspector, or in code:

yourView.userInteractionEnabled = NO;

当你有一个带有按钮的透明视图时:

创建该视图的子类并覆盖 pointInside 方法。我这样做的方法是将按钮的框架作为属性给予子类,以便在 pointInside 方法中检查:

Make a subclass of that view and override the pointInside method. The way I do it, is to give the button's frame to the subclass as a property, to check for in the pointInside method:

- (BOOL)pointInside:(CGPoint)point withEvent:(UIEvent *)event {
    BOOL pointInside = NO;
    // Check if the touch is in the button's frame
    if (CGRectContainsPoint(_buttonFrame, point)) pointInside = YES;

    return pointInside;
}

希望有所帮助!

这篇关于如何在iOS中向superview发送触摸事件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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