如何有条件地拦截触控事件?

时间:2022-08-22 21:18:04

I'm writing a custom keyboard extension for iOS 8 in Swift, and I'm having trouble implementing a Swype-style scribble overlay. Right now, my keyboard features a base UIView (the view property of my UIInputViewController) that has a number of UIButtons corresponding to keys on the keyboard. Each of these keys responds to touchUpInside events, and has UISwipeGestureRecognizers that input secondary characters when swiped up or down. I've overridden touchesBegan:withEvent: to add my scribble overlay view as a subview and touchesEnded:withEvent: to remove my scribble overlay view in my UIInputViewController.

我正在用Swift为iOS 8编写一个自定义键盘扩展,我在实现swype风格的潦草字体覆盖方面遇到了麻烦。现在,我的键盘有一个基本的UIView(我的UIInputViewController的视图属性),它有许多与键盘上的键对应的uibutton。每个键都响应touchUpInside事件,并具有uiswipegesturerecnizer,当上下滑动时输入辅助字符。我已经重写了touchesBegan:withEvent:添加我的scribble叠加视图作为子视图和touchesEnded:withEvent:在UIInputViewController中删除我的scribble叠加视图。

Currently, if I attempt to scribble starting from some of the spacing between buttons, the scribble overlay view appears properly. However, if I start scribbling from on top of a UIButton, the UIButton captures the touch event and doesn't forward it to my UIInputViewController.

目前,如果我试图从按钮之间的一些间隔开始进行划线,则会正确地显示scribble overlay视图。但是,如果我开始在UIButton的顶部涂鸦,UIButton捕获了触摸事件并且没有将它转发给我的UIInputViewController。

How can I implement my keyboard in such a way that my scribble overlay view appears if the user scribbles between buttons, but handles button presses/swipes otherwise?

如果用户在按钮之间乱涂乱画,而处理按钮按下/刷卡,我如何实现我的键盘呢?

--EDIT--

——编辑

I've attempted to resolve this by overriding the touchesBegan/Moved/Ended/Cancelled methods to forward the event to the next responder. If I click on a button and then click on another, I can get a scribble to appear between the two. However, I still can't get anything to draw on the button as a scribble between buttons. Anyone have a better idea?

我试图通过重写touchesBegan/移动/结束/取消方法来将事件转发给下一个响应器来解决这个问题。如果我点击一个按钮,然后点击另一个按钮,我可以在两者之间显示一个潦草的字迹。然而,我仍然无法在按钮上画出任何东西,就像在按钮之间涂鸦一样。谁有更好的主意?

--EDIT #2--

——编辑# 2

Currently, my view hierarchy is set up like this: UIView (from the UIInputViewController) Subviews: All of my buttons, then my ScribbleView overlaying the subviews.

目前,我的视图层次结构是这样设置的:UIView(来自UIInputViewController)子视图:我所有的按钮,然后是我的ScribbleView覆盖子视图。

If I set userInteractionEnabled to true on my ScribbleView, I can get scribbling to appear anywhere, regardless of the starting location, but then I lose my button functionality.

如果我在我的ScribbleView中设置userInteractionEnabled为true,我就可以在任何地方涂写,不管起始位置是什么,但是我就失去了按钮功能。

As for source code, I've been experimenting with various configurations of passing touching events and whatnot, but the main body of code can be found here: https://github.com/Kamaros/ELDeveloperKeyboard.

至于源代码,我一直在试验各种传递触摸事件之类的配置,但是代码主体可以在这里找到:https://github.com/Kamaros/ELDeveloperKeyboard。

2 个解决方案

#1


0  

I'm guessing here, as I didn't try to work with UIInputViewControllers yet. But how about subclassing UIButton and then overriding the touchesBegan:withEvent: and touchesEnded:withEvent: methods in order to forward these events to your UIInputViewController?

我猜这里,因为我还没有尝试使用UIInputViewControllers。但是子类化UIButton然后重写touchesBegan:withEvent:和touchesEnded:withEvent:方法以便将这些事件转发到UIInputViewController?

#2


0  

I think you need to set your ScribbleView interationEnabled to YES and forward to the button below the touchEvents you don't want. For example, if you recognize a long press in the same spot or a tap then you know the user wants to press the button.

我认为您需要将您的ScribbleView interationEnabled设置为YES并转发到您不想要的touchEvents下面的按钮。例如,如果您识别出在同一地点长按或轻按,那么您就知道用户想要按按钮。

#1


0  

I'm guessing here, as I didn't try to work with UIInputViewControllers yet. But how about subclassing UIButton and then overriding the touchesBegan:withEvent: and touchesEnded:withEvent: methods in order to forward these events to your UIInputViewController?

我猜这里,因为我还没有尝试使用UIInputViewControllers。但是子类化UIButton然后重写touchesBegan:withEvent:和touchesEnded:withEvent:方法以便将这些事件转发到UIInputViewController?

#2


0  

I think you need to set your ScribbleView interationEnabled to YES and forward to the button below the touchEvents you don't want. For example, if you recognize a long press in the same spot or a tap then you know the user wants to press the button.

我认为您需要将您的ScribbleView interationEnabled设置为YES并转发到您不想要的touchEvents下面的按钮。例如,如果您识别出在同一地点长按或轻按,那么您就知道用户想要按按钮。