我想使用 - (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event
在屏幕上返回一个自定义对象,变量完好无损。
我试过:
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch = [touches anyObject];
CGPoint location = [touch locationInView:self.view];
SLDSlide *selected = (SLDSlide *)[self hitTest:location withEvent:event];
}
和
SLDSlide *selected = (SLDSlide *)[super hitTest:location withEvent:event];
但它告诉我这些方法没有@interface
,我不确定如何自己编写一个返回我想要的对象的方法 (SLDSlide
,它是 UIImageView
的子类)。
最佳答案
以下代码将在 View Controller 的 self.view
中的给定点找到最前面的 SLDSlide
对象。
SLDSlide *selected = nil;
for ( UIView *view in self.view.subviews )
{
if ( [view isKindOfClass:[SLDSlide class]] )
if ( CGRectContainsPoint( view.frame, location ) )
selected = (SLDSlide *)view;
}
if ( selected )
NSLog( @"found one: %@", selected );
请注意,如果 View 层次结构有多个级别,那么代码将需要通过层次结构递归搜索,因此这仅适用于所有 SLDSlide
对象都是 View 的 subview 的情况 Controller 的 self.view
。
关于ios - 使用 HitTest :withEvent: to return custom object,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22586047/