【发布时间】:2014-01-15 17:00:01
【问题描述】:
我正在做一个绘图应用程序,我正在尝试平滑我用手指绘制的线条。为此,我正在使用“quadCurveToPoint”函数。但是,我没有做对。
下面是我的代码:
- (void) drawRect:(CGRect)rect
{
[path stroke];
}
- (id) initWithFrame:(CGRect)frame
{
if (self = [super initWithFrame:frame])
self.multipleTouchEnabled = NO;
path = [UIBezierPath bezierPath];
path.lineWidth = IS_IPAD? 2.0f : 1.0f;
return self;
}
- (void) touchesBegan:(NSSet *) touches withEvent:(UIEvent *) event
{
UITouch *touch = [touches anyObject];
m_previousPoint1 = [touch locationInView:self];
m_previousPoint2 = [touch locationInView:self];
m_currentPoint = [touch locationInView:self];
}
//Find the midpoint
CGPoint midPoint(CGPoint p1, CGPoint p2)
{
return CGPointMake((p1.x + p2.x) * 0.5, (p1.y + p2.y) * 0.5);
}
- (void) touchesMoved:(NSSet *) touches withEvent:(UIEvent *) event
{
UITouch *touch = [touches anyObject];
m_previousPoint2 = m_previousPoint1;
m_previousPoint1 = m_currentPoint;
m_currentPoint = [touch locationInView:self];
CGPoint mid1 = midPoint(m_previousPoint1, m_previousPoint2);
CGPoint mid2 = midPoint(m_currentPoint, m_previousPoint1);
[path setFlatness:1.0f];
[path setLineCapStyle:kCGLineCapRound];
[path setLineJoinStyle:kCGLineJoinRound];
[path moveToPoint:m_previousPoint1];
[path addLineToPoint:mid1];
[path addQuadCurveToPoint:mid2 controlPoint:m_currentPoint];
[self setNeedsDisplay];
}
- (void) touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
}
当我用手指绘图时,我得到如下路径:
我不明白我错过了什么。所以我需要这方面的帮助。
编辑:图片发布 w.r.t.到下面的答案。
谢谢 兰吉特
【问题讨论】:
标签: ios objective-c uikit core-graphics uibezierpath