iOS开发笔记-55: 菜单栏,中间按钮,超出父视图部分,无法响应按钮点击事件。同样的道理:扩大按钮响应范围

//中间按钮超出父视图范围,解决办法
- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event {
    UIView *view = [super hitTest:point withEvent:event];
    if (view == nil) {
        //中间按钮超出父视图
        JJTabBarItem *tabBarItem = _btnArray[2];
        // 转换坐标系
        CGPoint newPoint = [tabBarItem convertPoint:point fromView:self];
        // 判断触摸点是否在button上
        if (CGRectContainsPoint(tabBarItem.bounds, newPoint)) {
            view = tabBarItem;
        }
    }
    return view;
}

同样的道理:扩大按钮响应范围

#import 

@interface JJExpansionBtn : UIButton
//扩展的大小
@property (nonatomic, assign) CGFloat widthUP;
@property (nonatomic, assign) CGFloat heightUP;
//徽章
@property (nonatomic,assign) NSInteger badgeValue;
@property (nonatomic,strong) UIColor   *badgeBgColor;

@end
#import "JJExpansionBtn.h"

@interface JJExpansionBtn ()

@property (nonatomic,strong) UILabel *badgeView;

@end


@implementation JJExpansionBtn

- (instancetype)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
    
    }
    return self;
}

- (BOOL)pointInside:(CGPoint)point withEvent:(nullable UIEvent *)event {
    //响应宽度扩大54,高度扩大64
    CGFloat height = _heightUP;
    CGFloat width = _widthUP;
    return CGRectContainsPoint(JJBtnHitTestingBounds(self.bounds, width, height), point);
}
CGRect JJBtnHitTestingBounds(CGRect bounds, CGFloat minimumHitTestWidth, CGFloat minimumHitTestHeight) {
    CGRect hitTestingBounds = bounds;
    if (minimumHitTestWidth > bounds.size.width) {
        hitTestingBounds.size.width = minimumHitTestWidth;
        hitTestingBounds.origin.x -= (hitTestingBounds.size.width - bounds.size.width)/2;
    }
    if (minimumHitTestHeight > bounds.size.height) {
        hitTestingBounds.size.height = minimumHitTestHeight;
        hitTestingBounds.origin.y -= (hitTestingBounds.size.height - bounds.size.height)/2;
    }
    return hitTestingBounds;
}

你可能感兴趣的:(iOS开发笔记-55: 菜单栏,中间按钮,超出父视图部分,无法响应按钮点击事件。同样的道理:扩大按钮响应范围)