三角矩形背景 三角位于矩形下方

import "BaccaratGameBubbleView.h"

@implementation BaccaratGameBubbleView

// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.

  • (void)drawRect:(CGRect)rect {
    [super drawRect:rect];
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSaveGState(context);
CGMutablePathRef contourPath = CGPathCreateMutable();

CGFloat x = rect.size.width/2;
CGFloat y = rect.size.height;
CGFloat arrowWidth = 10.0f;
CGFloat arrowHeight = 5.0f;
CGFloat cornerRadius = 4.0f;

CGRect bubbleFrame = CGRectMake(0, 0, rect.size.width, rect.size.height-arrowHeight);

CGPathMoveToPoint(contourPath, NULL, x, y);
CGPathAddLineToPoint(contourPath, NULL, x - arrowWidth / 2, y + (-1) * arrowHeight);

[self drawBubbleBottomShapeWithFrame:bubbleFrame cornerRadius:cornerRadius path:contourPath];

CGPathAddLineToPoint(contourPath, NULL, x + (arrowWidth/2), y - arrowHeight);


CGPathCloseSubpath(contourPath);

CGContextAddPath(context, contourPath);
CGContextClip(context);

// 填充内部颜色
CGContextSetFillColorWithColor(context, [[UIColor blackColor] colorWithAlphaComponent:0.65].CGColor);
CGContextFillRect(context, CGRectMake(0, 0, rect.size.width, rect.size.height));

[self drawBorderWithBorderPath:contourPath context:context];
CGContextRestoreGState(context);

}

  • (void)drawBorderWithBorderPath:(CGMutablePathRef)borderPath context:(CGContextRef)context {
    // 设置画笔颜色
    CGContextAddPath(context, borderPath);
    CGContextSetStrokeColorWithColor(context, [[UIColor whiteColor] colorWithAlphaComponent:0.75].CGColor);
    CGContextSetLineWidth(context, 2);
    CGContextStrokePath(context);
    }

  • (void)drawBubbleBottomShapeWithFrame:(CGRect)frame cornerRadius:(CGFloat)cornerRadius path:(CGMutablePathRef)path {
    CGPathAddArcToPoint(path, NULL, frame.origin.x, frame.origin.y+frame.size.height, frame.origin.x, frame.origin.y, cornerRadius);
    CGPathAddArcToPoint(path, NULL, frame.origin.x, frame.origin.y, frame.origin.x+frame.size.width, frame.origin.y, cornerRadius);
    CGPathAddArcToPoint(path, NULL, frame.origin.x + frame.size.width, frame.origin.y, frame.origin.x+frame.size.width, frame.origin.y+frame.size.height, cornerRadius);
    CGPathAddArcToPoint(path, NULL, frame.origin.x + frame.size.width, frame.origin.y + frame.size.height, frame.origin.x, frame.origin.y+frame.size.height, cornerRadius);
    }

https://blog.csdn.net/samuelandkevin/article/details/121580726

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容