//中间按钮超出父视图范围,解决办法
- (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 <UIKit/UIKit.h>
@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;
}
网友评论