? 在網(wǎng)上查資料的時(shí)候,看到帶箭頭的UIView的箭頭都是直的,顯得很沒(méi)有平滑感,恰好當(dāng)時(shí)正在寫高德地圖,高德地圖中有一個(gè)氣泡就是畫的,正好拿來(lái)用一用!本來(lái)參考高德地圖的氣泡寫過(guò)一個(gè),不小心將代碼遺失去,后來(lái)參考:http://www.lxweimin.com/p/0c609bf5cb6f
下面是經(jīng)過(guò)改變路徑后的代碼:
- (void)drawRect:(CGRect)rect {
? ? ? ? //默認(rèn)圓角角度
? ? ? ? ?float ? ?r =4;
? ? ? ?//居中偏移量(箭頭高度)
? ? ? ?float ? ? ?offset =5;
? ? ? ?//設(shè)置箭頭位置
? ? ? ?float ? ? ? positionNum =20;
? ? ? ?//設(shè)置畫線長(zhǎng)寬
? ? ? ? float ? ? ? ?w =self.frame.size.width;
? ? ? ? float ? ? ? ? h =self.frame.size.height;
? ? ? ? //獲取文本
? ? ? ? CGContextRefcontext =UIGraphicsGetCurrentContext();
? ? ? ? ?//設(shè)置邊線寬度
? ? ? ? ?CGContextSetLineWidth(context,0.2);
? ? ? ? ?//邊框顏色
? ? ? ? ?CGContextSetStrokeColorWithColor(context, [UIColorgrayColor].CGColor);?
? ? ? ? ?//矩形填充顏色
? ? ? ? ? CGContextSetFillColorWithColor(context, [UIColor ? ? blueColor].CGColor);
? ? ? ? ? CGContextMoveToPoint(context,0, h);
? ? ? ? ?CGContextAddLineToPoint(context, w, h);//向右劃線
? ? ? ? ?CGContextAddLineToPoint(context, w, offset);//向右上角劃線
? ? ? ? ?CGContextAddLineToPoint(context, positionNum +20, offset);//向左劃線
? ? ? ? ?CGContextAddArcToPoint(context, positionNum +10,0, positionNum, offset, r);//向 ? ? ? ? ? ? 左上劃曲線
? ? ? ? ?CGContextAddArcToPoint(context, positionNum, offset,0, offset, r);//向左下劃曲線
? ? ? ? ?CGContextAddLineToPoint(context,0, offset);//向左頂點(diǎn)劃 ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ??
? ? ? ? ?CGContextDrawPath(context,kCGPathFillStroke);//根據(jù)坐標(biāo)繪制路徑
? ? ? ? /**父類調(diào)用放在畫完邊線后.不然設(shè)置的文字會(huì)被覆蓋*/
? ? ? ? ?[super ? drawRect:rect];
?}