【问题标题】:How do you detect touches in specific UIView when sliding finger across mutiple UIViews在多个视图中滑动手指时如何检测特定 UIView 中的触摸
【发布时间】:2011-05-20 22:02:35
【问题描述】:

我试图解决的场景是我有几个 UIViews 作为主 UIView 的子视图。我想要做的是突出显示一个视图矩形,让我们说当手指在视图上滑动时更改它的边框......类似于将鼠标光标移动/拖动到超文本链接上时发生的情况。

【问题讨论】:

  • stackoverflow.com/questions/6006851/… - 将 UITapGestureRecognizer 替换为 UIPanGestureRecognizer
  • 谢谢@TheBlack 我猜这与使用 touchesMoved 的工作方式相同,我的问题似乎是在父视图上进行手势时检测手指下的哪个子视图。

标签: ios objective-c uiview gestures


【解决方案1】:

您可以在touchesMoved: 方法中获取触摸的位置,然后询问主 UIView 哪个视图被触摸:

- (void)touchesMoved: (NSSet *)touches withEvent:(UIEvent *)event {
    UIView *subview = [masterView hitTest:[[touches anyObject] locationInView:masterView] withEvent:nil];
...
}

【讨论】:

  • 感谢highlycaffeinated,我已经尝试过了,但是父 UIView 上的 touchesMoved 可以进行触摸,但是如果您在此视图中开始触摸并滑过子视图,则子 UIView 似乎不会收到 touchesbegan或 touchesmoved 事件,因为此父视图获取所有内容。
  • 对,一旦在父视图中有了触摸的位置,就可以使用hitTest方法询问父视图该点对应的子视图中的哪个。如果这不起作用,您可以发布您尝试过的代码吗?
  • @highlycaffeinated code - (void) touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event { UITouch *touch = [[event allTouches] anyObject]; CGPoint location = [touch locationInView: touch.view]; AbstractAnnotation *subview = (AbstractAnnotation *)[self hitTest:[[touches anyObject] locationInView:self] withEvent:nil]; if ([subview respondsToSelector:@selector(AnnotationSelected)]) { NSLog(@"移过子视图"); [子视图 AnnotationSelected]; }; NSLog(@"AnnotationComponent touchesBegan x=%f y=%f", location.x, location.y); }
  • 您将位置作为相对于 touch.view 的 CGPoint,但随后您使用 locationInView self 调用 hitTest。如果 self == touch.view,这将起作用,但如果它们是不同的视图,则不会。
  • 另外,如果 self 是您的主视图,您应该在调用 [touch locationInView:] 时使用 self。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2014-06-21
  • 2019-10-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-05-06
相关资源
最近更新 更多