#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
CALayer自定义绘图
最新推荐文章于 2022-07-17 13:19:20 发布