123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105 |
- //
- // LDTabbar.m
- // YouHuiProject
- //
- // Created by liuxueli on 2019/2/25.
- // Copyright © 2019 kuxuan. All rights reserved.
- //
- #import "LDTabbar.h"
- #define MCTabBarItemHeight 49.0f
- #define AddButtonMargin 15
- @interface LDTabbar()
- //指向中间“+”按钮
- @end
- @implementation LDTabbar
- - (instancetype)init{
- if (self = [super init]){
- [self initView];
- }
- return self;
- }
- - (void)initView{
- _centerBtn = [UIButton buttonWithType:UIButtonTypeCustom];
- //去除选择时高亮
- _centerBtn.adjustsImageWhenHighlighted = NO;
- _centerBtn.backgroundColor =[UIColor whiteColor];
-
- [self addSubview:_centerBtn];
- }
- // 设置layout
- - (void)layoutSubviews {
- [super layoutSubviews];
- switch (self.position) {
- case MCTabBarCenterButtonPositionCenter:
- _centerBtn.frame = CGRectMake(([UIScreen mainScreen].bounds.size.width - _centerWidth)/2.0, (MCTabBarItemHeight - _centerHeight)/2.0 + self.centerOffsetY, _centerWidth, _centerHeight);
- break;
- case MCTabBarCenterButtonPositionBulge:
- _centerBtn.frame = CGRectMake(([UIScreen mainScreen].bounds.size.width - _centerWidth)/2.0, -_centerHeight/2.5 + self.centerOffsetY, _centerWidth, _centerHeight);
- break;
- default:
- break;
- }
- // 线的路径
- //第一个参数是起点,是圆形的圆心
- //第二个参数是半径
- //第三个参数是起始弧度
- //第四个参数是结束弧度
- //第五个参数是传入yes是顺时针,no为顺时针,下面的另外一种实现方法的参数意思也是一致
- //path addArcWithCenter:<#(CGPoint)#> radius:<#(CGFloat)#> startAngle:<#(CGFloat)#> endAngle:<#(CGFloat)#> clockwise:<#(BOOL)#>
-
- CGPoint viewCenter = CGPointMake(_centerBtn.frame.size.width / 2.0, _centerBtn.frame.size.height / 2.0); // 画弧的中心点,相对于view
- UIBezierPath *path = [UIBezierPath bezierPathWithArcCenter:viewCenter radius:_centerWidth/2 startAngle:-0.18 endAngle:3.32 clockwise:NO];
- CAShapeLayer *pathLayer = [CAShapeLayer layer];
- pathLayer.lineWidth = 1;
- pathLayer.strokeColor = [UIColor colorWithRed:226/255.2 green:226/255.0 blue:226/255.0 alpha:1].CGColor;
- pathLayer.fillColor = nil; // 默认为blackColor
- pathLayer.path = path.CGPath;
- [_centerBtn.layer addSublayer:pathLayer];
- _centerBtn.layer.cornerRadius=_centerWidth/2;
- _centerBtn.layer.masksToBounds=YES;
- [self drawRect:CGRectMake(([UIScreen mainScreen].bounds.size.width - _centerWidth)/2.0, - _centerWidth/2.0, _centerWidth, _centerWidth)];
- }
- - (void)setCenterImage:(UIImage *)centerImage {
- _centerImage = centerImage;
- // 如果设置了宽高则使用设置的大小
- if (self.centerWidth <= 0 && self.centerHeight <= 0){
- //根据图片调整button的位置(默认居中,如果图片中心在tabbar的中间最上部,这个时候由于按钮是有一部分超出tabbar的,所以点击无效,要进行处理)
- _centerWidth = centerImage.size.width+10;
- _centerHeight = centerImage.size.height+10;
- }
- [_centerBtn setImage:centerImage forState:UIControlStateNormal];
- }
- - (void)setCenterSelectedImage:(UIImage *)centerSelectedImage {
- _centerSelectedImage = centerSelectedImage;
- [_centerBtn setImage:centerSelectedImage forState:UIControlStateSelected];
- }
- //处理超出区域点击无效的问题
- - (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event{
- if (self.hidden){
- return [super hitTest:point withEvent:event];
- }else {
- //转换坐标
- CGPoint tempPoint = [self.centerBtn convertPoint:point fromView:self];
- //判断点击的点是否在按钮区域内
- if (CGRectContainsPoint(self.centerBtn.bounds, tempPoint)){
- //返回按钮
- return _centerBtn;
- }else {
- return [super hitTest:point withEvent:event];
- }
- }
- }
- @end
|