猎豆优选

LDTabbar.m 3.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. //
  2. // LDTabbar.m
  3. // YouHuiProject
  4. //
  5. // Created by liuxueli on 2019/2/25.
  6. // Copyright © 2019 kuxuan. All rights reserved.
  7. //
  8. #import "LDTabbar.h"
  9. #define MCTabBarItemHeight 49.0f
  10. #define AddButtonMargin 15
  11. @interface LDTabbar()
  12. //指向中间“+”按钮
  13. @end
  14. @implementation LDTabbar
  15. - (instancetype)init{
  16. if (self = [super init]){
  17. [self initView];
  18. }
  19. return self;
  20. }
  21. - (void)initView{
  22. _centerBtn = [UIButton buttonWithType:UIButtonTypeCustom];
  23. //去除选择时高亮
  24. _centerBtn.adjustsImageWhenHighlighted = NO;
  25. _centerBtn.backgroundColor =[UIColor whiteColor];
  26. [self addSubview:_centerBtn];
  27. }
  28. // 设置layout
  29. - (void)layoutSubviews {
  30. [super layoutSubviews];
  31. switch (self.position) {
  32. case MCTabBarCenterButtonPositionCenter:
  33. _centerBtn.frame = CGRectMake(([UIScreen mainScreen].bounds.size.width - _centerWidth)/2.0, (MCTabBarItemHeight - _centerHeight)/2.0 + self.centerOffsetY, _centerWidth, _centerHeight);
  34. break;
  35. case MCTabBarCenterButtonPositionBulge:
  36. _centerBtn.frame = CGRectMake(([UIScreen mainScreen].bounds.size.width - _centerWidth)/2.0, -_centerHeight/2.5 + self.centerOffsetY, _centerWidth, _centerHeight);
  37. break;
  38. default:
  39. break;
  40. }
  41. // 线的路径
  42. //第一个参数是起点,是圆形的圆心
  43. //第二个参数是半径
  44. //第三个参数是起始弧度
  45. //第四个参数是结束弧度
  46. //第五个参数是传入yes是顺时针,no为顺时针,下面的另外一种实现方法的参数意思也是一致
  47. //path addArcWithCenter:<#(CGPoint)#> radius:<#(CGFloat)#> startAngle:<#(CGFloat)#> endAngle:<#(CGFloat)#> clockwise:<#(BOOL)#>
  48. CGPoint viewCenter = CGPointMake(_centerBtn.frame.size.width / 2.0, _centerBtn.frame.size.height / 2.0); // 画弧的中心点,相对于view
  49. UIBezierPath *path = [UIBezierPath bezierPathWithArcCenter:viewCenter radius:_centerWidth/2 startAngle:-0.18 endAngle:3.32 clockwise:NO];
  50. CAShapeLayer *pathLayer = [CAShapeLayer layer];
  51. pathLayer.lineWidth = 1;
  52. pathLayer.strokeColor = [UIColor colorWithRed:226/255.2 green:226/255.0 blue:226/255.0 alpha:1].CGColor;
  53. pathLayer.fillColor = nil; // 默认为blackColor
  54. pathLayer.path = path.CGPath;
  55. [_centerBtn.layer addSublayer:pathLayer];
  56. _centerBtn.layer.cornerRadius=_centerWidth/2;
  57. _centerBtn.layer.masksToBounds=YES;
  58. [self drawRect:CGRectMake(([UIScreen mainScreen].bounds.size.width - _centerWidth)/2.0, - _centerWidth/2.0, _centerWidth, _centerWidth)];
  59. }
  60. - (void)setCenterImage:(UIImage *)centerImage {
  61. _centerImage = centerImage;
  62. // 如果设置了宽高则使用设置的大小
  63. if (self.centerWidth <= 0 && self.centerHeight <= 0){
  64. //根据图片调整button的位置(默认居中,如果图片中心在tabbar的中间最上部,这个时候由于按钮是有一部分超出tabbar的,所以点击无效,要进行处理)
  65. _centerWidth = centerImage.size.width+10;
  66. _centerHeight = centerImage.size.height+10;
  67. }
  68. [_centerBtn setImage:centerImage forState:UIControlStateNormal];
  69. }
  70. - (void)setCenterSelectedImage:(UIImage *)centerSelectedImage {
  71. _centerSelectedImage = centerSelectedImage;
  72. [_centerBtn setImage:centerSelectedImage forState:UIControlStateSelected];
  73. }
  74. //处理超出区域点击无效的问题
  75. - (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event{
  76. if (self.hidden){
  77. return [super hitTest:point withEvent:event];
  78. }else {
  79. //转换坐标
  80. CGPoint tempPoint = [self.centerBtn convertPoint:point fromView:self];
  81. //判断点击的点是否在按钮区域内
  82. if (CGRectContainsPoint(self.centerBtn.bounds, tempPoint)){
  83. //返回按钮
  84. return _centerBtn;
  85. }else {
  86. return [super hitTest:point withEvent:event];
  87. }
  88. }
  89. }
  90. @end