如下红色方形Button只有中间的黄色区域可以触发

继承UIButton创建CoustomButtom,.m中代码
- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event { if (!self.userInteractionEnabled || [self isHidden] || self.alpha <= 0.01) { return nil; } if ([self pointInside:point withEvent:event]) { __block UIView *hit = nil; [self.subviews enumerateObjectsWithOptions:NSEnumerationReverse usingBlock:^(__kindof UIView * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) { CGPoint vonvertPoint = [self convertPoint:point toView:obj]; hit = [obj hitTest:vonvertPoint withEvent:event]; if (hit) { *stop = YES; } }]; if (hit) { return hit; } else{ return self; } } else{ return nil; } } - (BOOL)pointInside:(CGPoint)point withEvent:(UIEvent *)event { CGFloat x1 = point.x; CGFloat y1 = point.y; // 方形btn中心 CGFloat x2 = self.frame.size.width / 2; CGFloat y2 = self.frame.size.height / 2; // 计算point距离中心点的位置 double dis = sqrt((x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2)); if (dis <= self.frame.size.width / 2) { return YES; } else { return NO; } }