ios - 使用 HitTest :withEvent: to return custom object

标签 ios objective-c cocoa-touch

我想使用 - (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/

相关文章:

ios - 在 Swift Playground 中使用 AVPlayer 播放视频?

ios - 崩溃报告 EXC_CRASH (SIGABRT)

ios - 如何知道父类(super class)指定的初始值设定项?

ios - 基于 block 的动画不起作用 : Animations and Completion Blocks are Executed Immediately (Without Duration)

ios - 如何在 IOS 中使用 mapkit 在 map 上显示用户

ios - Swift3 - '[String : AnyObject]' 不可转换为 '[HTTPCookiePropertyKey : Any]'

objective-c - 从 UIButton 中获取 UILabel

iphone - 按顺序播放多媒体内容

ios - 选中的单元格图像将显示在 UITableView 中

ios - iPhone : How to convert a photo into a pencil drawing