CALayer自定义绘图

#import "kUIView.h"
#import "kClayer.h"

#define kLayerWidth 185

@implementation kUIView
- (instancetype)initWithFrame:(CGRect)frame {
  self = [super initWithFrame:frame];
  if (self) {
    kClayer *layer = [[kClayer alloc] init];
    layer.bounds = CGRectMake(0, 0, kLayerWidth, kLayerWidth);
    layer.position = CGPointMake(160, 284);
    layer.backgroundColor = [UIColor colorWithRed:0 green:146 / 255.0 blue:1.0 alpha:1.0].CGColor;
    // 显示layrer
    [layer setNeedsDisplay];
    [self.layer addSublayer:layer];

  }
  
  return self;
}

- (void)drawRect:(CGRect)rect {
  [super drawRect:rect];
}

- (void)drawLayer:(CALayer *)layer inContext:(CGContextRef)ctx {
  [super drawLayer:layer inContext:ctx];
}

#import "kClayer.h"

@implementation kClayer
- (void)drawInContext:(CGContextRef)ctx {
  // 设置图层的颜色
  CGContextSetRGBFillColor(ctx, 135.0 / 255.0, 232.0 / 255.0, 84.0 / 255.0, 1);
  //图层边缘弧线的颜色
  CGContextSetRGBStrokeColor(ctx, 135.0 / 255.0, 232.0 / 255.0, 84.0 / 255.0, 1);
  
  CGContextMoveToPoint(ctx, 94.5, 33.5);
  
  // 画五角星
  CGContextAddLineToPoint(ctx, 104.02, 47.39);
  CGContextAddLineToPoint(ctx, 120.18, 52.16);
  CGContextAddLineToPoint(ctx, 109.91, 65.51);
  CGContextAddLineToPoint(ctx, 110.37, 82.34);
  CGContextAddLineToPoint(ctx, 94.5, 76.7);
  CGContextAddLineToPoint(ctx, 78.63, 82.34);
  CGContextAddLineToPoint(ctx, 79.09, 65.51);
  CGContextAddLineToPoint(ctx, 68.82, 52.16);
  CGContextAddLineToPoint(ctx, 84.98, 47.39);
  CGContextClosePath(ctx);
  
  // 进行描边
  CGContextDrawPath(ctx, kCGPathFillStroke);
}
@end

#import "CustomLayerGraphicController.h"
#import "kClayer.h"
#import "kUIView.h"

@interface CustomLayerGraphicController ()

@end

@implementation CustomLayerGraphicController

- (void)viewDidLoad {
  [super viewDidLoad];
  kUIView *view = [[kUIView alloc] initWithFrame:[UIScreen mainScreen].bounds];
  view.backgroundColor = [UIColor colorWithRed:110.0 / 255.0 green:249 / 255.0 blue:249.0 / 255.0 alpha:1];

  [self.view addSubview:view];
}
@end

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值