您的位置:首页 > 移动开发 > IOS开发

iOS Core Graphics手写板

2016-07-17 01:15 363 查看
//
//  HJView.m
//  CGContextDemo2
//
//  Created by 黄健 on 16/7/16.
//  Copyright © 2016年 黄健. All rights reserved.
//

#import "HJView.h"

@interface HJView ()

@property (nonatomic, assign) CGMutablePathRef path;    // 可变路径

@end

@implementation HJView

- (instancetype)initWithCoder:(NSCoder *)aDecoder
{
if (self = [super initWithCoder:aDecoder])
{
self.path = CGPathCreateMutable();
}
return self;
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
CGPoint position = [touches.anyObject locationInView:self];

CGPathMoveToPoint(self.path, nil, position.x, position.y);
}

- (void)touchesMoved:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
CGPoint position = [touches.anyObject locationInView:self];

CGPathAddLineToPoint(self.path, nil, position.x, position.y);

[self setNeedsDisplay];
}

- (void)drawRect:(CGRect)rect
{
CGContextRef context = UIGraphicsGetCurrentContext();

CGContextAddPath(context, self.path);

CGContextSetLineWidth(context, 5);

CGContextStrokePath(context);
}

@end


内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  ios core graphics