iOS图形绘制

-------------------简单线条、图形-------------------

#import "MyShapeView.h"

@implementation MyShapeView

/*
 这个方法在绘制视图对象的时候调用
 如果需要调用这个方法里面的代码,我们不能直接调用drawRect方法
 只能调用-(void)setNeedsDisplay方法,系统会自动调用drawRect方法
 */
-(void)drawRect:(CGRect)rect
{
    //获取上下文
    CGContextRef ctx = UIGraphicsGetCurrentContext();
    
    //1.直线
    //设置绘制直线的颜色
    /*
     第一个参数:上下文
     第二~五个参数:颜色值
     */
    //CGContextSetRGBStrokeColor(ctx, 220.0f/255.0f, 220.0f/255.0f, 220.0f/255.0f, 1.0f);
    //设置线宽度
    CGContextSetLineWidth(ctx, 10);
    
    //设置直线的端点
    const CGPoint points1 [] = {CGPointMake(30, 80),CGPointMake(140, 80)};
    //绘制直线
    /*
     第一个参数:上下文
     第二个参数:直线的端点
     第三个参数:数组的元素个数
     */
    //sizeof(points1)/sizeof(points1[0])
    CGContextStrokeLineSegments(ctx, points1, 2);
    
    //2.矩形
    //1)空心
    /*
     第一个参数:上下文
     第二个参数:颜色值
     */
    CGContextSetStrokeColorWithColor(ctx, [UIColor redColor].CGColor);
    //设置线宽
    CGContextSetLineWidth(ctx, 2);
    
    //绘制
    /*
     第一个参数:上下文
     第二个参数:矩形的大小
     */
    CGContextStrokeRect(ctx, CGRectMake(40, 100, 80, 40));
    
    //2)实心矩形
    //设置颜色
    CGContextSetFillColorWithColor(ctx, [UIColor purpleColor].CGColor);
    //设置线宽
    CGContextSetLineWidth(ctx, 4);
    //绘制
    CGContextFillRect(ctx, CGRectMake(160, 100, 80, 40));
    
    //3.椭圆
    //1)空心
    //颜色
    CGContextSetStrokeColorWithColor(ctx, [UIColor greenColor].CGColor);
    //线宽
    CGContextSetLineWidth(ctx, 8);
    //绘制
    /*
     第一个参数:上下文
     第二个参数:椭圆是矩形的内切椭圆
     */
    CGContextStrokeEllipseInRect(ctx, CGRectMake(40, 200, 100, 80));
    
    //2)实心
    //颜色
    CGContextSetFillColorWithColor(ctx, [UIColor cyanColor].CGColor);
    //线宽
    CGContextSetLineWidth(ctx, 2);
    //绘制
    CGContextFillEllipseInRect(ctx, CGRectMake(160, 200, 120, 80));
    
    //4.虚线
    //颜色
    CGContextSetStrokeColorWithColor(ctx, [UIColor brownColor].CGColor);
    //线宽
    CGContextSetLineWidth(ctx, 2);
    
    //设置点线规则
    /*
     第一个参数:上下文
     第二个参数:相位
     第三个参数:点线规则
     第四个参数:数组元素的个数
     */
    const CGFloat dash1[] = {20, 20};
    
    CGContextSetLineDash(ctx, 0, dash1, 2);
    
    //绘制虚线
    const CGPoint points2 [] = {CGPointMake(40, 300),CGPointMake(240, 300)};
    CGContextStrokeLineSegments(ctx, points2, 2);
    
    //设置点线规则
    const CGFloat dash2 [] = {10, 20, 10};
    
    CGContextSetLineDash(ctx, 0, dash2, 3);
    
    //绘制虚线
    const CGPoint points3[] = {CGPointMake(40, 360),CGPointMake(240, 360)};
    CGContextStrokeLineSegments(ctx, points3, 2);
    
    //相位
    const CGFloat dash3 [] = {20, 20};
    CGContextSetLineDash(ctx, 10, dash3, 2);
    
    //绘制虚线
    const CGPoint points4[] = {CGPointMake(40, 420),CGPointMake(240, 420)};
    CGContextStrokeLineSegments(ctx, points4, 2);
}

绘制圆角,正多边形

#import "ComplexView.h"

@implementation ComplexView


-(void)drawRect:(CGRect)rect
{
    //获取上下文
    CGContextRef ctx = UIGraphicsGetCurrentContext();
    
    [self geneShapex:30 y:80 w:120 h:80 r:20 ctx:ctx];
    
    //
    CGContextStrokePath(ctx);
    
    
    //绘制正多边形
    [self geneComplexShapex:160 y:300 r:80 num:10 ctx:ctx];
    
    CGContextStrokePath(ctx);
    
}

//绘制圆角矩形
/*
 @param x:矩形左上角的x坐标
 @param y:矩形左上角的y坐标
 @param w:矩形的宽度
 @param h:矩形的高度
 @param r:弧线的半径
 @param ctx:上下文
 */
- (void)geneShapex:(CGFloat)x y:(CGFloat)y w:(CGFloat)w h:(CGFloat)h r:(CGFloat)r ctx:(CGContextRef)ctx
{
    
    //移到起始点
    CGContextMoveToPoint(ctx, x+r, y);
    
    //直线
    CGContextAddLineToPoint(ctx, x+w-r, y);
    //CGContextAddLineToPoint(<#CGContextRef  _Nullable c#>, <#CGFloat x#>, <#CGFloat y#>)
    //弧线
    /*
     第一个参数:上下文
     第二个参数:第一个控制点的横坐标
     第三个参数:第一个控制点的纵坐标
     第四个参数:第二个控制点的横坐标
     第五个参数:第二个控制点的纵坐标
     第六个参数:弧线的半径
     */
    CGContextAddArcToPoint(ctx, x+w, y, x+w, y+r, r);
    
    //直线
    CGContextAddLineToPoint(ctx, x+w, y+h-r);
    //弧线
    CGContextAddArcToPoint(ctx, x+w, y+h, x+w-r, y+h, r);
    
    //直线
    CGContextAddLineToPoint(ctx, x+r, y+h);
    //弧线
    CGContextAddArcToPoint(ctx, x, y+h, x, y+h-r, r);
    
    //直线
    CGContextAddLineToPoint(ctx, x, y+r);
    //弧线
    CGContextAddArcToPoint(ctx, x, y, x+r, y, r);
    
    
}


//绘制正多边形
/*
 @param x0:多边形的中心点x坐标
 @param y0:多边形的中心点y坐标
 @param r:多边形的外接圆半径
 @param num:几边形
 @param ctx:上下文
 */
- (void)geneComplexShapex:(CGFloat)x0 y:(CGFloat)y0 r:(CGFloat)r num:(int)num ctx:(CGContextRef)ctx
{
    CGContextMoveToPoint(ctx, x0+r, y0);
    
    //绘制直线
    for (int i=1; i<=num; i++) {
        
        CGContextAddLineToPoint(ctx, x0+r*cos(2*M_PI*i/num), y0+r*sin(2*M_PI*i/num));
        
    }
}


-------------------绘制文字-------------------

Snip20170918_1.png

#import "MyView.h"

@implementation MyView

-(void)drawRect:(CGRect)rect
{
    //绘制文字
    NSString *str = @"hello,text";
    
    CGPoint point1 = CGPointMake(30, 80);
    
    NSDictionary *dict = @{NSForegroundColorAttributeName:[UIColor redColor],NSFontAttributeName:[UIFont systemFontOfSize:20]};
    
    /*
     第一个参数:文字开始绘制的位置
     第二个参数:文字的属性
     */
    [str drawAtPoint:point1 withAttributes:dict];
    
    //绘制空心文字
    CGContextRef ctx = UIGraphicsGetCurrentContext();
    
    //kCGTextStroke表示绘制空心文字
    CGContextSetTextDrawingMode(ctx, kCGTextStroke);
    
    //绘制文字
    NSString *str2 = @"hello, shanghai";
    [str2 drawAtPoint:CGPointMake(30, 120) withAttributes:@{NSFontAttributeName:[UIFont systemFontOfSize:28],NSForegroundColorAttributeName:[UIColor cyanColor]}];
    
    //设置阴影
    /*
     第一个参数:上下文
     第二个参数:阴影的偏移量
     第三个参数:透明度
     第四个参数:阴影的颜色
     */
    CGContextSetShadowWithColor(ctx, CGSizeMake(2, 2), 1, [UIColor blackColor].CGColor);

    //绘制文字
    NSString *str3 = @"hello, objective-C";
    [str3 drawAtPoint:CGPointMake(30, 300) withAttributes:@{NSFontAttributeName:[UIFont systemFontOfSize:30],NSForegroundColorAttributeName:[UIColor redColor]}];
    
    
}

绘制UIImage

- (UIImage *)generateImage
{
    //开始在内存中绘制图片
    UIGraphicsBeginImageContext(CGSizeMake(300, 400));
      
    //获取上下文
    CGContextRef ctx = UIGraphicsGetCurrentContext();
    
    //直线
    //线宽
    CGContextSetLineWidth(ctx, 2);
    //颜色
    CGContextSetStrokeColorWithColor(ctx, [UIColor redColor].CGColor);
    //绘制
    const CGPoint points[] = {CGPointMake(40, 80),CGPointMake(240, 80)};
    CGContextStrokeLineSegments(ctx, points, 2);
    
    
    //显示资源图片
    UIView *myView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 300, 400)];
    
    
    UIImageView *imgView = [[UIImageView alloc] initWithFrame:CGRectMake(80, 100, 40, 40)];
    imgView.image = [UIImage imageNamed:@"mark"];
    //绘制
    [myView addSubview:imgView];
    
    [myView.layer renderInContext:ctx];
    
    
    //获取图片
    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();

    UIGraphicsEndImageContext();
    
    return image;
}
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • 图形绘制在iOS开发中很常见,处于工作的需要和自身爱好,我就把最近自己使用过的图形绘制的简单方式稍微总结了一下 ,...
    见哥哥长高了阅读 4,355评论 1 7
  • 今天做页面需要用到绘图绘制虚线,由于平时不怎么用到绘图,对绘图的基础也不是很好,所以写一篇关于绘图的文章,巩固下基...
    daihz阅读 8,173评论 0 0
  • 独立CGPath总结 与CGContext关联的Path总结 点&线 注意 AddLineToPoint实现方式在...
    LiYaoPeng阅读 8,617评论 1 14
  • UIkit 在实际开发过程中使用频率较低,这里我就贴上ppt 算了. iOS的图形系统 UIview的视觉属性 U...
    AAup阅读 1,382评论 0 0
  • 八七 感谢能有感情这么好的发小 感谢学长的够意思 感谢大雨天 记:还是心态吧,把心态放正的话遇到何事都没事,再穷可...
    XBIN阅读 2,383评论 0 0

友情链接更多精彩内容