主页 The Standard Way to Write hitTest
Post
Cancel

The Standard Way to Write hitTest

Preface

This article carries strong personal feelings. If you feel uncomfortable reading it, please close it as soon as possible. This article is only for personal learning records. Reposting or sharing within the scope of the license is welcome. Please respect the copyright and keep the original link. Thank you for your understanding and cooperation. If you think this site can help you, you can subscribe to this site via RSS. Thanks for your support!

The Standard Way to Write HitTest

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event
{
    if (!self.isUserInteractionEnabled || self.isHidden || self.alpha <= 0.01)
    {
        return nil;
    }
    if ([self pointInside:point withEvent:event])
    {
        for (UIView *subview in [self.subviews reverseObjectEnumerator])
        {
            CGPoint convertedPoint = [subview convertPoint:point fromView:self];
            UIView *hitTestView = [subview hitTest:convertedPoint withEvent:event];
            if (hitTestView)
            {
                if (hitTestView.superview == self)
                {
                    // None of self's subviews should respond to events
                    return nil;
                }
                return hitTestView;
            }
        }
        return nil;
    }
    return nil;
}

Summary

Noting a problem that came up in code review — this is the standard way to write hitTest.

该博客文章由作者通过 CC BY 4.0 进行授权。

The Smoothstep Function

Kerning Text Layout Calculation