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