002-Custom Drawing

2016-12-21  本文已影响52人  紫荆秋雪_文

1、Custom Drawing

2、示例如下

#import "ViewController.h"
//#import <QuartzCore/QuartzCore.h>

@interface ViewController ()<CALayerDelegate>
/**
 *  layerView
 */
@property (nonatomic, strong) UIView *layerView;

/**
 *  blueLayer
 */
@property (nonatomic, strong) CALayer *blueLayer;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // 背景颜色
    self.view.backgroundColor = [UIColor darkGrayColor];
    // View
    self.layerView = [[UIView alloc] initWithFrame:CGRectMake(100, 100, 200, 200)];
    self.layerView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:self.layerView];
    //blueLayer
    self.blueLayer = [CALayer layer];
    self.blueLayer.frame = CGRectMake(50, 50, 100, 100);
    self.blueLayer.backgroundColor = [UIColor blueColor].CGColor;
    self.blueLayer.contentsScale = [UIScreen mainScreen].scale;
    self.blueLayer.delegate = self;
    [self.layerView.layer addSublayer:self.blueLayer];
    [self.blueLayer display];
}

- (void)drawLayer:(CALayer *)layer inContext:(nonnull CGContextRef)ctx {
    //设置宽度
    CGContextSetLineWidth(ctx, 5.0);
    //设置颜色
    CGContextSetStrokeColorWithColor(ctx, [UIColor redColor].CGColor);
    //设置rect
//    CGContextStrokeRect(ctx, layer.bounds);
    CGContextStrokeEllipseInRect(ctx, layer.bounds);
}

效果如下

QQ20161221-2@2x.png

3、小结

上一篇 下一篇

猜你喜欢

热点阅读