iOS - Detecting touches in a UIView?

前端 未结 4 556
无人共我
无人共我 2021-01-12 01:55

So I have a Subclass of UIView that is suppose to detect touches. The view detect touches only if the touches started inside the current view. When the touches start outside

4条回答
  •  遥遥无期
    2021-01-12 02:30

    The following solution worked. I have multiple instances of MyCustomView; as the touches move I want to detect the views that are being touched

    I ended up moving touch detection from MyCustomView to its superView, so the following code is no longer in MyCustomView class:

    - (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))
            {
    
            }
        }
    }
    

提交回复
热议问题