浅析OC中drawRect

废话不多说,直接上代码

  • 在View中重写- (void)drawRect:(CGRect)rect这个方法
@interface DrawView(){
        UIImage *image1;
        UIImage* image2;
    }
    @end
- (instancetype)initWithFrame:(CGRect)frame
    {
        self = [super initWithFrame:frame];
        if (self) {
             image1 = [UIImage imageNamed:@"image.png"];
             image2 = [UIImage imageNamed:@"BGG.png"];
        }
        return self;
    }
- (void)drawRect:(CGRect)rect
    {
    //    [self drawText];
    //    [self drawLine];
    //    [self draeRectangle:CGRectMake(10, 100, 50, 50)];
    //    [self drawImage];
    //    [self animationImage];
    }

1.先从简单的画线开始

-(void)drawLine
    {
    CGContextRef context    =UIGraphicsGetCurrentContext();//获取画布
    CGContextSetStrokeColorWithColor(context, [UIColor redColor].CGColor);//线条颜色
    CGContextSetShouldAntialias(context,NO);//设置线条平滑,不需要两边像素宽
    CGContextSetLineWidth(context,1.0f);//设置线条宽度
    CGContextMoveToPoint(context,153,10); //线条起始点
    CGContextAddLineToPoint(context,253,10);//线条结束点
    CGContextStrokePath(context);//结束,也就是开始画
    }
    

2.绘制文本

-(void)drawText
    {
        UIColor *color =[UIColor colorWithRed:0.5f
                                               green:0.0f
                                                blue:0.5f
                                               alpha:1.0f];
        [color set];
        UIFont *withFont:helvetica = [UIFont fontWithName:@"HelveticaNeue-Bold"size:30.0f];
        NSString *string =@"李先森";
        [string drawAtPoint:CGPointMake(25,190)withFont:helvetica];
    }

3.绘制矩形:这里有分为两种风格{1.无框矩形2.有框矩形}

- (void)draeRectangle:(CGRect)rect{
    //首先,获取上下文
    CGContextRef context = UIGraphicsGetCurrentContext();
    /*
     //画无框矩形
    //设置矩形填充颜色:红色
    CGContextSetRGBFillColor(context, 1.0, 0.0, 0.0, 1.0);
    //填充矩形
    CGContextFillRect(context, rect);
    //执行绘画
    CGContextStrokePath(context);
    */
    
    //画有框矩形
    //设置矩形填充颜色:红色
    CGContextSetRGBFillColor(context, 1.0, 0.0, 0.0, 1.0);
    //填充矩形
    CGContextFillRect(context, rect);
    //设置画笔颜色:黑色
    CGContextSetStrokeColorWithColor(context, BLUECOLOR.CGColor);
    //设置画笔线条粗细
    CGContextSetLineWidth(context, 3.0);
    //画矩形边框
    CGContextAddRect(context,rect);
    //执行绘画  
    CGContextStrokePath(context);
    }

4.绘制图片

-(void)drawImage{
        CGContextRef context    =UIGraphicsGetCurrentContext();//获取画布
        
    //    CGContextDrawImage(context,CGRectMake(160,0,160, 150), [image1 CGImage]);
    //上面这种方式,绘制出来的图片是翻转的,开始不知道。因为测试的图片都比较对称。后发发现是上下颠倒了
    
        //下面才是正确的方法。
        UIGraphicsPushContext( context );
    //    [image1 drawInRect:CGRectMake(160, 0, 160, 150)];
        [image1 drawInRect:CGRectMake(160, 0, 160, 150) blendMode:kCGBlendModeColor alpha:1];
    
        UIGraphicsPopContext();
    }

5.给图片添加动画

- (void)animationImage{
        UIImageView *imageView = [[UIImageView alloc]initWithImage:image2];
        imageView.frame =CGRectMake(100,100,100,100);
        imageView.layer.cornerRadius = 50;
        imageView.layer.masksToBounds = YES;
        [self addSubview:imageView];
        imageView.userInteractionEnabled =YES;
        UITapGestureRecognizer* singleTap =
        [[UITapGestureRecognizer  alloc]initWithTarget:self action:@selector(onImageClick)];
        [imageView addGestureRecognizer:singleTap];
        
        //animation
        CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"transform"];
        animation.delegate =self;
        animation.toValue = [NSValue valueWithCATransform3D:CATransform3DMakeRotation(M_PI ,0, 0,1.0)];
        animation.duration =1;
        animation.cumulative =YES;
        animation.repeatCount =INT_MAX;
        
        [imageView.layer addAnimation:animation forKey:@"animation"];
    }
    - (void)onImageClick
    {
    // someing code   
    }
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Quartz2D以及drawRect的重绘机制字数1487 阅读21 评论1 喜欢1一、什么是Quartz2D Q...
    PurpleWind阅读 845评论 0 3
  • --绘图与滤镜全面解析 概述 在iOS中可以很容易的开发出绚丽的界面效果,一方面得益于成功系统的设计,另一方面得益...
    韩七夏阅读 2,838评论 2 10
  • 在iOS中随处都可以看到绚丽的动画效果,实现这些动画的过程并不复杂,今天将带大家一窥ios动画全貌。在这里你可以看...
    每天刷两次牙阅读 8,617评论 6 30
  • 现在自己最主要的事情就是好好学习,努力把初级考过,一定要过。。加油!
    简1阅读 134评论 0 0
  • 窗外霾重重,冬意迟珊。 路遇不识是红颜。 雾里不知身何处,一往无前。 无事莫出门,边塞霾都, 离家容易归家难。 雾...
    手握瓷杯阅读 202评论 3 7