我正在制作一款绘图应用程序,用户可以用手指或者笔来绘制/写字。为此,我在我的应用程序中参考了
我面临的问题是,有时候当你写得很接近时,书写不太流畅。
因此,我认为我在获取控制点方面犯了一些错误。
以下是我的代码:
//找到中点
根据我的理解,在
https://github.com/yusenhan/Smooth-Line-View
的代码。我面临的问题是,有时候当你写得很接近时,书写不太流畅。
因此,我认为我在获取控制点方面犯了一些错误。
以下是我的代码:
//找到中点
CGPoint midPoint(CGPoint p1, CGPoint p2)
{
return CGPointMake((p1.x + p2.x) * 0.5, (p1.y + p2.y) * 0.5);
}
#pragma mark Gesture handle
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *touch = [touches anyObject];
//LocationInView returns the current location of the reciever in coordinate system of the given View.
m_previousPoint1 = [touch locationInView:self];
m_previousPoint2 = [touch locationInView:self];
m_currentPoint = [touch locationInView:self];
[self touchesMoved:touches withEvent:event];}
-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
//AnyObject:- Returns one of the objects in the set, or nil if the set contains no objects.
UITouch *touch = [touches anyObject];
m_previousPoint2 = m_previousPoint1;
m_previousPoint1 = m_currentPoint;
m_currentPoint = [touch locationInView:self];
if(m_drawStep != ERASE)
{
m_drawStep = DRAW;
m_drawing = TRUE;
}
}
-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
UIGraphicsBeginImageContextWithOptions(self.bounds.size, NO, 0.0);//creates a graphics context suitable for use as an image(size of the image,opquae,scale, if scale = 0.0, means platform will take care of scaling)
[self.layer renderInContext:UIGraphicsGetCurrentContext()];
m_curImage = UIGraphicsGetImageFromCurrentImageContext();// to turn the context into a UIImage
UIGraphicsEndImageContext();
}
- (void)drawRect:(CGRect)rect
{
CGPoint mid1 = midPoint(m_previousPoint1, m_previousPoint2);
CGPoint mid2 = midPoint(m_currentPoint, m_previousPoint1);
[m_curImage drawAtPoint:CGPointMake(0, 0)];
CGContextRef context = UIGraphicsGetCurrentContext();//Get a reference to current context(The context to draw)
[self.layer renderInContext:context];
//Simply keep referring to this context in below functions with proper arguments.
CGContextMoveToPoint(context, mid1.x, mid1.y);//Position the current point
CGContextAddQuadCurveToPoint(context, m_previousPoint1.x, m_previousPoint1.y, mid2.x, mid2.y);
CGContextSetLineCap(context, kCGLineCapRound);
CGContextSetBlendMode(context, kCGBlendModeNormal);
CGContextSetLineJoin(context, kCGLineJoinRound);
CGContextSetLineWidth(context, self.lineWidth);
CGContextSetStrokeColorWithColor(context, self.lineColor.CGColor);
CGContextSetShouldAntialias(context, YES);
CGContextSetAllowsAntialiasing(context, YES);
CGContextSetAlpha(context, self.lineAlpha);
CGContextSetFlatness(context, 0.6);
CGContextStrokePath(context);//paints(fills) the line along the current path.
}
根据我的理解,在
CGContextAddQuadCurveToPoint
中传递的点可以进行微调,但我不知道如何进行微调。我阅读文档以查找Bezier Curve
的控制点,但我不理解。因此,朋友们,请帮助我理解我哪里出错了。