swift畫圓角對話框

這裏的圓角的對話框 我指的是類似安卓的.9.png那樣的圓角的對話框,就是這個樣子的。


分解出來就是一個實心的圓角矩形加一個三角形,外面用直線描邊,裏面寫一些居中懸浮的字符串而已。

1.畫圓角矩形  假設x1,y1爲右下角的點

let rx:CGFloat =4      //圓角弧度
CGContextSetFillColorWithColor(context, UIColor(red: 248/255, green:248/255, blue:248/255, alpha:1).CGColor)//設置畫筆顏色
CGContextMoveToPoint(context, x1, y1 - rx);                         // 右下角
CGContextAddArcToPoint(context, x1, y1, x2, y1, rx);               // 右下角度-左下角
CGContextAddArcToPoint(context, x2, y1, x2, y2, rx);               // 左下角-左上角
CGContextAddArcToPoint(context, x2, y2, x1, y2, rx);                //左上角-右上角
CGContextAddArcToPoint(context, x1, y2, x1, y1, rx);                //右上角-右下腳
CGContextClosePath(context);//封起來
CGContextDrawPath(context, CGPathDrawingMode.Fill)   //根據座標繪製路徑

2.畫三角形

取圓角矩形的底邊上某個點(xs,y1),這裏隨便舉個栗子啦   這裏x2<xs<x1

let sPoint = [CGPointMake(xs, y1),CGPointMake(xs + 10, y1),CGPointMake(xs + 5, y1+5)];
CGContextAddLines(context, sPoints5, 3);//添加線
CGContextClosePath(context);//封起來
CGContextDrawPath(context, CGPathDrawingMode.Fill)//根據座標繪製路徑

這樣基本輪廓就出來啦,一個尖尖向下的對話框


3.描邊

就是畫線咯,注意在三角形區域不要畫錯喔

CGContextSetLineWidth(context, 0.5)//設置畫筆寬度
CGContextSetStrokeColorWithColor(context, UIColor(red: 204/255, green:204/255, blue:204/255, alpha:1).CGColor)//設置畫筆顏色
CGContextMoveToPoint(context, x1, y1);           //起點
CGContextAddLineToPoint(context, xs+10, y1);
CGContextAddLineToPoint(context, xs+5, y1+5);   //斜邊1
CGContextAddLineToPoint(context, xs, y1);        //斜邊2
CGContextAddLineToPoint(context, x2, y1);
CGContextAddLineToPoint(context, x2, y2);
CGContextAddLineToPoint(context, x1, y2);
CGContextAddLineToPoint(context, x1, y1);
CGContextStrokePath(context) //關閉路徑

寫到這裏,萌萌噠圓角對話框就寫好咯,加下來在裏面挖坑寫字符串就可以了


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章