0

我有几个 UIView,它们的形状有些复杂,我通过每个 UIView 的 drawRect: 方法绘制了这些形状。我保存了对路径的引用(通过 CGPath 属性),并在我的触摸方法中使用它来检测触摸是否实际上在路径或形状(CGPathContainsPoint)内。由于形状的原因,视图的某些区域是透明的(显然,任何不是正方形/矩形的区域都将具有透明区域,假设视图的 backgroundColor 属性设置为 clearColor 或类似的)。

对于视图最终位于顶部或相互重叠的情况(经常发生),我正在测试触摸方法(touchesBegan 等)以查看触摸是否实际上在 CGPath 或形状内。如果触摸不在形状内,我会将触摸转发到下面的下一个视图,以查看它是否反过来通过了该要求。

可悲的是(令我沮丧的是),这与实际绑定到视图的触摸行为不同。我已经以各种方式解决了这个问题,除了一个特别的问题。每个视图都使用 UIRotationGestureRecognizer。但是,如果触摸源自另一个视图的透明区域,并且该视图将该触摸转发到其下方的视图,则永远不会触发 UIGestureRecognizer。

我已经从我知道的各个角度看待这个问题。我试图想办法将触摸重新分配或绑定到适当的视图,但我不知道这是否可能。只要视图只是将触摸转发到另一个视图,触摸将保持绑定到首先收到触摸的原始视图。

我想知道您是否可以通过子类化 UIApplication 或 UIWindow 并覆盖 sendEvent:" 方法来执行此操作。我不确定 Apple 在这方面可能会给您什么控制权。我无法做任何可能访问私有的事情API's. 我真的只需要一些方法来重新分配触摸,以便视图认为触摸属于它。

有任何想法吗?'

4

1 回答 1

2

You could do this by either subclassing UIView or subclassing UIGestureRecognizer.

Subclassing UIView

By subclassing UIView (or any of its subclasses) and overriding

- (BOOL)pointInside:(CGPoint)point withEvent:(UIEvent *)event

you can decide what points are considered to be inside your view. For points outside the path you simply return NO

Subclassing UIGestureRecognizer

By subclassing UIGestureRecognizer (or any of its subclasses) and overriding

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event

you can decide what touches the gesture recognizer should recognize. For all touches outside the path you should call (but not subclass)

- (void)ignoreTouch:(UITouch *)touch forEvent:(UIEvent *)event

on yourself.


Point inside of path?

In either case you need to know if the points is inside of the path that you say you've stored.

If the paths are already stored as UIBezierPaths you can simply call

- (BOOL)containsPoint:(CGPoint)point

On the bezier path. The point or the touch may need to be converted to the same view coordinates as the path by calling

- (CGPoint)convertPoint:(CGPoint)point toView:(UIView *)view

or

- (CGPoint)convertPoint:(CGPoint)point fromView:(UIView *)view

On your view.

If the path is stored as a CGPath you can create a UIBezierPath from it using

[UIBezierPath bezierPathWithCGPath:CGPath]; 
于 2012-05-11T19:06:57.607 回答