iOS-为iPhone应用程序绘制铅笔笔划
我正在开发一个iOS-为iPhone应用程序绘制铅笔笔划,ios,objective-c,core-graphics,Ios,Objective C,Core Graphics,我正在开发一个绘图应用程序,在那里我需要实现铅笔效果,类似于苹果笔记应用程序 因此,我将UIView子类化并编写以下代码 CGPoint midPoint(CGPoint p1, CGPoint p2) { return CGPointMake((p1.x + p2.x) * 0.5, (p1.y + p2.y) * 0.5); } @interface DrawingView () { CGPoint currentPoint; CGPoint previousPoi
绘图应用程序
,在那里我需要实现铅笔效果
,类似于苹果笔记应用程序
因此,我将UIView子类化并编写以下代码
CGPoint midPoint(CGPoint p1, CGPoint p2)
{
return CGPointMake((p1.x + p2.x) * 0.5, (p1.y + p2.y) * 0.5);
}
@interface DrawingView () {
CGPoint currentPoint;
CGPoint previousPoint1;
CGPoint previousPoint2;
CGMutablePathRef path;
}
@end
@implementation DrawingView
- (id)init
{
self = [super init];
if (self != nil) {
path = CGPathCreateMutable();
}
return self;
}
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *touch = [touches anyObject];
previousPoint1 = [touch previousLocationInView:self];
currentPoint = [touch locationInView:self];
}
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *touch = [touches anyObject];
previousPoint2 = previousPoint1;
previousPoint1 = [touch previousLocationInView:self];
currentPoint = [touch locationInView:self];
CGPoint mid1 = midPoint(previousPoint1, previousPoint2);
CGPoint mid2 = midPoint(currentPoint, previousPoint1);
CGMutablePathRef subpath = CGPathCreateMutable();
CGPathMoveToPoint(subpath, NULL, mid1.x, mid1.y);
CGPathAddQuadCurveToPoint(subpath, NULL, previousPoint1.x, previousPoint1.y, mid2.x, mid2.y);
CGRect bounds = CGPathGetBoundingBox(subpath);
CGPathAddPath(path, NULL, subpath);
CGPathRelease(subpath);
[self setNeedsDisplayInRect:bounds];
}
- (void)drawRect:(CGRect)rect
{
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextAddPath(context, path);
CGContextSetLineCap(context, kCGLineCapRound);
CGContextSetLineWidth(context, 5.0f);
CGContextSetStrokeColorWithColor(context, [UIColor lightGrayColor].CGColor);
CGContextSetBlendMode(context, kCGBlendModeNormal);
CGContextSetAlpha(context, 1.0f);
CGContextStrokePath(context);
}
这可以很好地工作,并产生如下输出
但是我需要像下图一样的输出。类似于Apple Notes应用程序
有什么办法可以做到这一点。有什么线索吗?有什么线索吗?