iOS - 检测UIView中的触摸?

ary*_*axt 9 uiview touchesmoved ios

所以我有一个UIView的子类,假设检测触摸.仅当触摸在当前视图内开始时,视图检测才会触摸.当触摸从视图外部开始并且它们在我的自定义视图中移动时,touchesMoved不会被调用.任何解决方案都可以检测当前视图中尚未启动的移动触摸?

@implementation MycustomView

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
   // This only gets called if touches have started in the current View
} 

@end
Run Code Online (Sandbox Code Playgroud)

ary*_*axt 20

以下解决方案有效.我有MyCustomView的多个实例; 当触摸移动时,我想检测被触摸的视图

我最终将MyCustomView的触摸检测移动到其superView,因此以下代码不再出现在MyCustomView类中:

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event 
{
    UITouch *touch = [[event allTouches] anyObject];
    CGPoint touchLocation = [touch locationInView:self.contentView];

    for (UIView *view in self.contentView.subviews)
    {
        if ([view isKindOfClass:[MyCustomView class]] &&
            CGRectContainsPoint(view.frame, touchLocation))
        {

        }
    }
}
Run Code Online (Sandbox Code Playgroud)