iOS: Non-square hit areas for buttons

2020-03-05 06:52发布

问题:

I need to make some triangular buttons that overlap each other.

While UIButtons can take transparent images as backgrounds, and UIControls can have custom views, the hit area of these is always square. How can I create a triangular hitarea for my buttons?

I come from a FLash background so I would normally create a hitarea for my view, but I don't believe I can do this in Cocoa.

Any tips?

回答1:

You can achieve this by subclassing UIButton and providing your own:

- (BOOL)pointInside:(CGPoint)point withEvent:(UIEvent *)event {
    // return YES if point is inside the receiver’s bounds; otherwise, NO.
}

Apple's UIView Documentation provides the details, such as confirming that point is already in the receiver's coordinate system.