iOS劃圓 冒個(gè)泡

最近忙于搞javaee企業(yè)級(jí)應(yīng)用,希望java服務(wù)端的前輩多多指教,故沒怎么更新iOS相關(guān),下面更新一個(gè)較為簡(jiǎn)單的iOS知識(shí):

iOS畫圓、畫線

UIView:

- (void)drawRect:(CGRect)rect {

[super drawRect:rect];

CGRect frame = CGRectMake(50, 100, 100, 100);

/*畫填充圓

*/

CGContextRef context = UIGraphicsGetCurrentContext();

[[UIColor whiteColor] set];

CGContextFillRect(context, rect);

CGContextAddEllipseInRect(context, frame);

[[UIColor orangeColor] set];

CGContextFillPath(context);

/*邊框圓

*/

CGContextSetRGBStrokeColor(context, 255/255.0, 106/255.0, 0/255.0, 1);

CGContextSetLineWidth(context, 5);

CGContextAddArc(context, 50, 70, 20, 0, 2*M_PI, 0);

CGContextDrawPath(context, kCGPathStroke);

/*畫直線

*/

CGRect markFrame = CGRectMake(100, 250, 200, 200);

[[UIColor orangeColor] set];

CGContextSetLineWidth(context, 4);

CGPoint points[5];

points[0] = CGPointMake(markFrame.origin.x,markFrame.origin.y);

points[1] = CGPointMake(markFrame.origin.x+markFrame.size.width/4, markFrame.origin.y+markFrame.size.height/2);

points[2] = CGPointMake(markFrame.origin.x+markFrame.size.width/2, markFrame.origin.y+5);

points[3] = CGPointMake(markFrame.origin.x+markFrame.size.width/4*3,markFrame.origin.y+markFrame.size.height/2);

points[4] = CGPointMake(markFrame.origin.x+markFrame.size.width, markFrame.origin.y);

CGContextAddLines(context, points, 5);

CGContextDrawPath(context, kCGPathStroke);

//邊框圓

CGContextSetLineWidth(context, 5);

CGContextAddArc(context, markFrame.origin.x+markFrame.size.width/2, markFrame.origin.y+markFrame.size.height/2, markFrame.size.height/2-2, 0, 2*M_PI, 0);

CGContextDrawPath(context, kCGPathStroke);

/*曲線

*/

[[UIColor redColor] set];

CGContextSetLineWidth(context, 4.0);

CGContextSetStrokeColorWithColor(context, [UIColor orangeColor].CGColor);

CGContextMoveToPoint(context, 300, 370);

CGContextAddCurveToPoint(context,? 193, 320, 100, 370, 100, 370);

CGContextStrokePath(context);

}

UIViewController:

#import "DrawViewController.h"

@interface DrawViewController ()

@end

@implementation DrawViewController

- (void)viewDidLoad {

[super viewDidLoad];

/*

*畫虛線圓

*/

CAShapeLayer *dotteLine =? [CAShapeLayer layer];

CGMutablePathRef dottePath =? CGPathCreateMutable();

dotteLine.lineWidth = 2.0f ;

dotteLine.strokeColor = [UIColor orangeColor].CGColor;

dotteLine.fillColor = [UIColor clearColor].CGColor;

CGPathAddEllipseInRect(dottePath, nil, CGRectMake(50.0f,? 50.0f, 200.0f, 200.0f));

dotteLine.path = dottePath;

NSArray *arr = [[NSArray alloc] initWithObjects:[NSNumber numberWithInt:10],[NSNumber numberWithInt:5], nil];

dotteLine.lineDashPhase = 1.0;

dotteLine.lineDashPattern = arr;

CGPathRelease(dottePath);

[self.view.layer addSublayer:dotteLine];

/*

*畫實(shí)線圓

*/

CAShapeLayer *solidLine =? [CAShapeLayer layer];

CGMutablePathRef solidPath =? CGPathCreateMutable();

solidLine.lineWidth = 2.0f ;

solidLine.strokeColor = [UIColor orangeColor].CGColor;

solidLine.fillColor = [UIColor clearColor].CGColor;

CGPathAddEllipseInRect(solidPath, nil, CGRectMake(50.0f,? 300.0f, 200.0f, 200.0f));

solidLine.path = solidPath;

CGPathRelease(solidPath);

[self.view.layer addSublayer:solidLine];

/*

*畫虛線

*/

CAShapeLayer *dotteShapeLayer = [CAShapeLayer layer];

CGMutablePathRef dotteShapePath =? CGPathCreateMutable();

[dotteShapeLayer setFillColor:[[UIColor clearColor] CGColor]];

[dotteShapeLayer setStrokeColor:[[UIColor orangeColor] CGColor]];

dotteShapeLayer.lineWidth = 2.0f ;

NSArray *dotteShapeArr = [[NSArray alloc] initWithObjects:[NSNumber numberWithInt:10],[NSNumber numberWithInt:5], nil];

[dotteShapeLayer setLineDashPattern:dotteShapeArr];

CGPathMoveToPoint(dotteShapePath, NULL, 20,500);

CGPathAddLineToPoint(dotteShapePath, NULL, 20, 285);

CGPathAddLineToPoint(dotteShapePath, NULL, 300,285);

[dotteShapeLayer setPath:dotteShapePath];

CGPathRelease(dotteShapePath);

[self.view.layer addSublayer:dotteShapeLayer];

/*

*畫實(shí)線

*/

CAShapeLayer *solidShapeLayer = [CAShapeLayer layer];

CGMutablePathRef solidShapePath =? CGPathCreateMutable();

[solidShapeLayer setFillColor:[[UIColor clearColor] CGColor]];

[solidShapeLayer setStrokeColor:[[UIColor orangeColor] CGColor]];

solidShapeLayer.lineWidth = 2.0f ;

CGPathMoveToPoint(solidShapePath, NULL, 20, 265);

CGPathAddLineToPoint(solidShapePath, NULL, 300,265);

CGPathAddLineToPoint(solidShapePath, NULL, 300,50);

[solidShapeLayer setPath:solidShapePath];

CGPathRelease(solidShapePath);

[self.view.layer addSublayer:solidShapeLayer];

}

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容