您的位置:首页 > 移动开发 > IOS开发

iOS中核心绘图Core Graphics的小试

2016-01-28 16:18 393 查看
- (void)drawRect:(CGRect)rect{
CGContextRef ctxt = UIGraphicsGetCurrentContext();
CGContextSaveGState(ctxt);
CGContextSetLineWidth(ctxt, 0.1f);
CGFloat radius = 8.0f;

if (_first) {
radius = 8.0f;
[[UIColor redColor] setFill];
}else{
radius = 5.0f;
[[UIColor lightGrayColor] setFill];
}

CGContextAddArc(ctxt, 17, 20, radius, 0, 2*M_PI, YES);
CGContextFillPath(ctxt);
CGContextRestoreGState(ctxt);
CGContextSaveGState(ctxt);

[[UIColor lightGrayColor] setStroke];
if (!_first) {
CGContextMoveToPoint(ctxt, 17, 0);
CGContextAddLineToPoint(ctxt, 17, 15);
}

if (_first) {
CGContextMoveToPoint(ctxt, 17, 28);
CGContextAddLineToPoint(ctxt, 17, self.height);
}
if (!_last && !_first) {//下面的线
CGContextMoveToPoint(ctxt, 17, 25);
CGContextAddLineToPoint(ctxt, 17, self.height);
}

CGContextStrokePath(ctxt);
CGContextRestoreGState(ctxt);

//方法二:
//    UIBezierPath *path = [UIBezierPath bezierPath];//贝塞尔会自动获取上下文
//
//    [path setLineWidth:0.2f];
//    CGFloat radius = 8.0f;
//    if (_first) {
//        radius = 8.0f;
//        [[UIColor redColor] setFill];
//    }else{
//        radius = 5.0f;
//        [[UIColor lightGrayColor] setFill];
//    }
//    [path addArcWithCenter:CGPointMake(17, 20) radius:radius startAngle:0 endAngle:360 clockwise:YES];
//    [path fill];
//
//
//    [[UIColor blackColor] setStroke];
//    if (!_first) {
//        [path moveToPoint:CGPointMake(17, 0)];
//        [path addLineToPoint:CGPointMake(17, 15)];
//    }
//
//    if (!_last) {
//        [path moveToPoint:CGPointMake(17, 25)];
//        [path addLineToPoint:CGPointMake(17, self.height)];
//    }
//
//    [path stroke];

}


效果:

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: