1.自定義控件

一. 圖片加水印

   -(void)watermark{
        UIImage *image = self.imageView.image;
        //1.第一個(gè)參數(shù):大小,第二個(gè)參數(shù):不透明度,第三個(gè)參數(shù):傳入0.0位當(dāng)前設(shè)備分辨率
        UIGraphicsBeginImageContextWithOptions(image.size, NO, 0.0);
        [image drawAtPoint:CGPointZero];
        NSString *text = @"片加水印圖片加水印圖片加水印圖片加水印圖片加水印片加水印圖片加水印圖片加水印圖片加水印圖片加水印片加水印圖片加水印圖片加水印圖片加水印圖片加水印片加水印圖片加水印圖片加水印圖片加水印圖片加水印";
        NSMutableDictionary *dict = [NSMutableDictionary dictionary];
        dict[NSFontAttributeName] = [UIFont systemFontOfSize:17];
        dict[NSForegroundColorAttributeName] = [UIColor redColor];
        [text drawInRect:CGRectMake(10, 200, image.size.width, image.size.height) withAttributes:dict];
        image = UIGraphicsGetImageFromCurrentImageContext();
        UIGraphicsEndImageContext();
        self.imageView.image = image;
}

二. 繪制圖片


- (void)drawRect:(CGRect)rect{
    [self.image drawInRect:rect];
}

- (void)setImage:(UIImage *)image{
    _image = image;
    [self setNeedsDisplay];
}

+ (instancetype)initWithImage:(UIImage *)image{
    ZQImageView *imageView = [[ZQImageView alloc]init];
    imageView.frame = CGRectMake(0, 0, image.size.width, image.size.height);
    imageView.image = image;
    return imageView;
}

三. 繪制文字


- (void)drawRect:(CGRect)rect{
    NSString *text = @"然后屏幕會(huì)從緩存區(qū)獲取了新的一幀圖像并顯示出來,與此同時(shí),CPU也開始了下一幀數(shù)據(jù)的計(jì)算";
    NSMutableDictionary *dict = [NSMutableDictionary  dictionary];
    //設(shè)置字體大小
    dict[NSFontAttributeName] = [UIFont systemFontOfSize:20];
    //設(shè)置字體前景色
    dict[NSForegroundColorAttributeName] = [UIColor redColor];
    //設(shè)置描邊顏色
    dict[NSStrokeColorAttributeName] = [UIColor blackColor];
    //設(shè)置描邊線寬
    dict[NSStrokeWidthAttributeName] = @1;
    
    NSShadow *shadow = [[NSShadow alloc]init];
    shadow.shadowColor = [UIColor grayColor];
    shadow.shadowOffset = CGSizeMake(5, 5);
    shadow.shadowBlurRadius = 3;
    //設(shè)置陰影
    dict[NSShadowAttributeName]  = shadow;
    //[text drawAtPoint:CGPointMake(0, 0) withAttributes:dict];//此方法文字不會(huì)換行
    [text drawInRect:rect withAttributes:dict];//此方法文字會(huì)自動(dòng)換行
    //也可以繪制,也會(huì)自動(dòng)換行
    //[text drawWithRect:rect options:NSStringDrawingUsesLineFragmentOrigin attributes:dict context:nil];
}

四.繪制簡(jiǎn)單圖形

    1. 繪制線條
-(void)drawLine:(CGRect)rect{
   CGContextRef ctx= UIGraphicsGetCurrentContext();
   UIBezierPath *path = [UIBezierPath bezierPath];
   //第一條線
   [path moveToPoint:CGPointMake(100, 100)];//起點(diǎn)
   [path addLineToPoint:CGPointMake(rect.size.width-100, rect.size.height-100)];//終點(diǎn)
   // 第二條線
   [path moveToPoint:CGPointMake(rect.size.width-100, 100)];//起點(diǎn)
   [path addLineToPoint:CGPointMake(100, rect.size.height-100)];//終點(diǎn)
   [path addLineToPoint:CGPointMake(120, rect.size.height*0.5+30)];//第三條線,起點(diǎn)為第二條線終點(diǎn)
   //  設(shè)置線寬
   CGContextSetLineWidth(ctx, 10);
   //設(shè)置拐角樣式 
   CGContextSetLineCap(ctx, kCGLineCapRound);
   //設(shè)置連接點(diǎn)樣式
   CGContextSetLineJoin(ctx, kCGLineJoinRound);
   //設(shè)置線條顏色
   [[UIColor redColor]set];
   //線條添加到上下文
   CGContextAddPath(ctx, path.CGPath);
   //繪制到view上
   CGContextStrokePath(ctx);
}
    1. 繪制曲線
-(void)drawClub:(CGRect)rect{
    CGContextRef ctx= UIGraphicsGetCurrentContext();
    UIBezierPath *path = [UIBezierPath bezierPath];
    [path moveToPoint:CGPointMake(10, rect.size.height*0.5)];
    [path addQuadCurveToPoint:CGPointMake(rect.size.width-10, rect.size.height*0.5) controlPoint:CGPointMake(rect.size.width*0.5,20)];
    CGContextAddPath(ctx, path.CGPath);
    CGContextStrokePath(ctx);
}
    1. 繪制圓?。ㄟM(jìn)度條)

- (void)drawRect:(CGRect)rect{
    UIBezierPath *path = [UIBezierPath bezierPathWithRect:rect];
    CGFloat radius = MIN(rect.size.width, rect.size.height)*0.5-10;
    CGPoint center = CGPointMake(rect.size.width*0.5, rect.size.height*0.5);
    [[UIColor redColor]set];
    [path stroke];
    path = [UIBezierPath bezierPathWithArcCenter:center radius:radius startAngle:-M_PI_2 endAngle:-M_PI_2+_progress*M_PI*2 clockwise:YES];
    [path addLineToPoint:center];
    [path fill];
}

- (void)setProgress:(CGFloat)progress{
    _progress = progress;
    [self setNeedsDisplay];
}

    1. 繪制餅圖
- (void)drawRect:(CGRect)rect{
   NSArray * array = @[@25,@25,@50,@35,@15,@45,@100];
   CGFloat sum = 0;
   for (NSNumber *num in array) {
       sum += num.intValue*1.0f;
   }
   CGFloat startAngle = 0;
   CGFloat endAngle = 0;
   for (NSNumber *num in array) {
       endAngle = startAngle + num.intValue /sum *M_PI*2;
       [self drawPan:rect startAngle:startAngle endAngle:endAngle];
       startAngle = endAngle;
   }
   
}

-(void)drawPan:(CGRect)rect  startAngle:(CGFloat)startAngle endAngle:(CGFloat)endAngle {
   CGPoint center = CGPointMake(rect.size.width*0.5, rect.size.height*0.5);
   CGFloat radius = MIN(rect.size.width, rect.size.height)*0.5-20;
   UIBezierPath *path = [UIBezierPath bezierPathWithArcCenter:center radius:radius startAngle:startAngle endAngle:endAngle clockwise:YES];
   [path addLineToPoint:center];
   [[self randomColor] set];
   [path fill];
}

-(UIColor*)randomColor{
   CGFloat r = arc4random_uniform(256)/255.0;
   CGFloat g = arc4random_uniform(256)/255.0;
   CGFloat b = arc4random_uniform(256)/255.0;
   return [UIColor colorWithRed:r green:g blue:b alpha:1.0];
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
   [self setNeedsDisplay];
}
    1. 繪制矩形

-(void)drawRect:(CGRect)rect{
    CGRect tempRect = CGRectMake(rect.size.width*0.5+10, 80, rect.size.width*0.5-20, rect.size.height*0.5-110);
    UIBezierPath * path = [UIBezierPath bezierPathWithRect:tempRect];
    [[UIColor redColor]set];
    [path stroke];
}
    1. 繪制圓

-(void)drawCircle:(CGRect)rect{
    CGFloat radius  = MIN(rect.size.width, rect.size.height)*0.5-20;
    CGRect tempRect = CGRectMake(10, rect.size.height*0.5+10, radius, radius);
    UIBezierPath * path = [UIBezierPath bezierPathWithRoundedRect:tempRect cornerRadius:radius];
    [[UIColor redColor]set];
    [path stroke];
}
  • 7.繪制圓角矩形

-(void)drawRoundRect:(CGRect)rect{
    CGRect tempRect = CGRectMake(10, 80, rect.size.width*0.5-20, rect.size.height*0.5-110);
    UIBezierPath * path = [UIBezierPath bezierPathWithRoundedRect:tempRect cornerRadius:10];
    [[UIColor redColor]set];
    [path stroke];
}
    1. 繪制扇形
-(void)drawFan:(CGRect)rect{
    CGFloat radius  = MIN(rect.size.width-20, rect.size.height-20)*0.25;
    CGPoint center = CGPointMake(rect.size.width*0.5+5+radius, rect.size.height*0.5+radius+10);
    UIBezierPath * path = [UIBezierPath bezierPathWithArcCenter:center radius:radius startAngle:0 endAngle:M_PI_2 clockwise:YES];
    [[UIColor redColor]set];
    [path addLineToPoint:center];
    [path stroke];
}
    1. 繪制過程中操作

- (void)drawRect:(CGRect)rect{
    UIBezierPath *path = [UIBezierPath bezierPathWithRect:rect];
    [[UIColor redColor]set];
    [path stroke];
    
    CGContextRef ctx = UIGraphicsGetCurrentContext();
    CGRect cgRect = CGRectMake(10, 10, rect.size.width-20, 100);
    path = [UIBezierPath bezierPathWithOvalInRect:cgRect];
    //平移
    CGContextTranslateCTM(ctx, 50, 50);
    //旋轉(zhuǎn)
    CGContextRotateCTM(ctx, M_PI_4);
    //縮放
    CGContextScaleCTM(ctx, 0.5, 0.5);
    CGContextAddPath(ctx, path.CGPath);
    CGContextFillPath(ctx);
}
  1. 定時(shí)器雪花下落效果

static float y = 0;

- (instancetype)initWithFrame:(CGRect)frame{
    if(self = [super initWithFrame:frame]){
        //[NSTimer scheduledTimerWithTimeInterval:0.01 target:self selector:@selector(changeValue) userInfo:nil repeats:YES];
        CADisplayLink *line = [CADisplayLink displayLinkWithTarget:self selector:@selector(changeValue)];
        [line addToRunLoop:[NSRunLoop mainRunLoop] forMode:NSDefaultRunLoopMode];
    }
    return self;
}

-(void)changeValue{
    y+=6;
    if(y>=[UIScreen mainScreen].bounds.size.height){
        y = 0;
    }
    [self setNeedsDisplay];
}


- (void)drawRect:(CGRect)rect{
    UIImage *image = [UIImage imageNamed:@"雪花"];
    [image drawAtPoint:CGPointMake(rect.size.width*0.5-image.size.width*0.5, y+image.size.height)];
}
最后編輯于
?著作權(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)容