美文网首页
简单的泡泡View

简单的泡泡View

作者: 淘气CC | 来源:发表于2023-04-07 15:22 被阅读0次

    简单的泡泡View:
    @interface UIPaopaoView : UIView

    @end

    @implementation UIPaopaoView{
    CGFloat kArrowHeight; //箭头高度
    CGFloat kArrowWidth; //箭头宽度
    CGFloat kCornerRadius; //圆角半径
    CGFloat kArrowPosition; //参与箭头起点计算的值
    CGFloat kCornerRadiusArrowPadding;//箭头与圆角间距
    }

    • (instancetype)initWithFrame:(CGRect)frame{
      self = [super initWithFrame:frame];
      if(self){
      //箭头高度
      kArrowHeight = 8.0;
      //箭头宽度
      kArrowWidth = 15.0;
      //圆角
      kCornerRadius = 10.0;
      //箭头与圆角间距
      kCornerRadiusArrowPadding = 10.0;
      //箭头位置居左
      kArrowPosition = kCornerRadius + kCornerRadiusArrowPadding;
      //箭头位置居中
      //kArrowPosition = 0.5 * self.frame.size.width - 0.5 * kArrowWidth;
      //箭头位置居右
      //kArrowPosition = self.frame.size.width - kCornerRadius - kArrowWidth - kCornerRadiusArrowPadding;

        //设置阴影颜色
        self.layer.shadowColor = [UIColor colorWithRed:0.0 green:0.0 blue:1.0 alpha:1.0].CGColor;
        //设置阴影不透明度
        self.layer.shadowOpacity = 0.5;
        //设置阴影偏移量
        self.layer.shadowOffset = CGSizeMake(0, 0);
        //设置阴影圆角
        self.layer.shadowRadius = 2.0;
        
        //泡泡
        UIView *paopao = [[UIView alloc] initWithFrame: self.bounds];
        paopao.backgroundColor = [UIColor whiteColor];
        paopao.layer.cornerRadius = 3.0;
        paopao.layer.masksToBounds = YES;
        paopao.layer.mask = [self drawPaoPaoViewMaskLayer:paopao];
        
        [self addSubview:paopao];
      

      }
      return self;
      }

    ///绘制遮罩层

    • (CAShapeLayer *)drawPaoPaoViewMaskLayer:(UIView *)paopaoView
      {
      CAShapeLayer *maskLayer = [CAShapeLayer layer];
      maskLayer.frame = paopaoView.bounds;
      //右上弧中心
      CGPoint topRightArcCenter = CGPointMake(self.frame.size.width - kCornerRadius, kArrowHeight + kCornerRadius);
      //左上弧中心
      CGPoint topLeftArcCenter = CGPointMake(kCornerRadius, kArrowHeight + kCornerRadius);
      //右下弧中心
      CGPoint bottomRightArcCenter = CGPointMake(self.frame.size.width - kCornerRadius, self.
      frame.size.height- kArrowHeight - kCornerRadius);
      //左下弧中心
      CGPoint bottomLeftArcCenter = CGPointMake(kCornerRadius, self.frame.size.height - kArrowHeight - kCornerRadius);

      UIBezierPath *path = [UIBezierPath bezierPath];
      //将接收器的当前点移动到指定位置
      [path moveToPoint: CGPointMake(0, kArrowHeight + kCornerRadius)];
      //向接收器的路径追加一条直线(左侧直线)
      [path addLineToPoint: CGPointMake(0, bottomLeftArcCenter.y)];
      //添加左下角弧线
      [path addArcWithCenter: bottomLeftArcCenter radius: kCornerRadius startAngle: -M_PI endAngle: -M_PI-M_PI_2 clockwise: NO];
      //指向下的箭头
      [path addLineToPoint: CGPointMake(kArrowPosition, self.frame.size.height - kArrowHeight)];
      [path addLineToPoint: CGPointMake(kArrowPosition + 0.5 * kArrowWidth, self.frame.size.height)];
      [path addLineToPoint: CGPointMake(kArrowPosition+kArrowWidth, self.frame.size.height - kArrowHeight)];
      [path addLineToPoint: CGPointMake(self.frame.size.width - kCornerRadius, self.frame.size.height - kArrowHeight)];

      //添加右下角弧线
      [path addArcWithCenter: bottomRightArcCenter radius: kCornerRadius startAngle: -M_PI-M_PI_2 endAngle: -M_PI * 2 clockwise: NO];
      //向接收器的路径追加一条直线(右侧直线)
      [path addLineToPoint: CGPointMake(self.frame.size.width, kArrowHeight+kCornerRadius)];
      //添加右上角弧线
      [path addArcWithCenter: topRightArcCenter radius: kCornerRadius startAngle: 0 endAngle: -M_PI_2 clockwise: NO];
      //指向上的箭头
      //[path addLineToPoint: CGPointMake(kArrowPosition + kArrowWidth, kArrowHeight)];
      //[path addLineToPoint: CGPointMake(kArrowPosition + 0.5 * kArrowWidth, 0)];
      //[path addLineToPoint: CGPointMake(kArrowPosition, kArrowHeight)];
      //[path addLineToPoint: CGPointMake(kCornerRadius, kArrowHeight)];

      //添加左上角弧线
      [path addArcWithCenter: topLeftArcCenter radius: kCornerRadius startAngle: -M_PI_2 endAngle: -M_PI clockwise: NO];
      //关闭路径
      [path closePath];

      maskLayer.path = path.CGPath;

      return maskLayer;
      }

    @end

    相关文章

      网友评论

          本文标题:简单的泡泡View

          本文链接:https://www.haomeiwen.com/subject/agwpddtx.html