Ios 带有CGContextStrokePath的虚线
我正在开发一个应用程序,用户应该能够用手指画线。用户可以启用虚线,但我在尝试这样做时遇到了一个奇怪的问题。如果用户缓慢拖动手指,会出现实线,但快速拖动时会出现虚线。我希望破折号在启用时无论出现什么。问题显示在下面的gif中: 我正在使用Ios 带有CGContextStrokePath的虚线,ios,objective-c,ios7,cgcontext,Ios,Objective C,Ios7,Cgcontext,我正在开发一个应用程序,用户应该能够用手指画线。用户可以启用虚线,但我在尝试这样做时遇到了一个奇怪的问题。如果用户缓慢拖动手指,会出现实线,但快速拖动时会出现虚线。我希望破折号在启用时无论出现什么。问题显示在下面的gif中: 我正在使用UIPangestureRecognitor收集接触点: -(void)dragGestureCaptured:(UIPanGestureRecognizer *)gesture { NSValue* touchPoint = [NSValue valu
UIPangestureRecognitor
收集接触点:
-(void)dragGestureCaptured:(UIPanGestureRecognizer *)gesture
{
NSValue* touchPoint = [NSValue valueWithCGPoint:[gesture locationInView:self.drawingView]];
if(gesture.state == UIGestureRecognizerStateBegan)
{
[self initializePreviousPointValues:[touchPoint CGPointValue]];
}
else if (gesture.state == UIGestureRecognizerStateEnded)
{
EBLog(@"Dragging ends..");
return;
}
previousPoint2 = previousPoint1;
previousPoint1 = currentPoint;
currentPoint = [touchPoint CGPointValue];
CGPoint mid1 = midPoint(previousPoint1, previousPoint2);
CGPoint mid2 = midPoint(currentPoint, previousPoint1);
[self.drawingView addDrawColor:self.currentDrawColor];
[self.drawingView.controlPoints addObject:[NSValue valueWithCGPoint:previousPoint1]];
[self.drawingView.points addObject:[NSValue valueWithCGPoint:mid2]];
[self.drawingView.movePoints addObject:[NSValue valueWithCGPoint:mid1]];
[self.drawingView setNeedsDisplay];
}
drawingView
使用controlsPoints
、points
和movePoints
绘制线条:
- (void)drawLinesInContext:(CGContextRef)context
{
for (int i = 0; i < [self.points count]; i++)
{
CGPoint movePoint = [[self.movePoints objectAtIndex:i] CGPointValue];
CGPoint controlPoint = [[self.controlPoints objectAtIndex:i] CGPointValue];
CGPoint point = [[self.points objectAtIndex:i] CGPointValue];
CGContextMoveToPoint(context, movePoint.x, movePoint.y);
CGContextAddQuadCurveToPoint(context, controlPoint.x, controlPoint.y, point.x, point.y);
NSNumber* colorHash = [self.colorHashes objectAtIndex:i];
UIColor* col = [self.colorsForHashValue objectForKey:colorHash];
CGFloat red = 0.0f, blue = 0.0f, green = 0.0f, alpha = 1.0f;
[col getRed:&red green:&green blue:&blue alpha:&alpha];
if (alpha == 0.0f)
{
penWidth = 15.0f;
CGContextSetBlendMode(UIGraphicsGetCurrentContext(), kCGBlendModeClear);
}
else
{
penWidth = 5.0f;
CGContextSetBlendMode(UIGraphicsGetCurrentContext(), kCGBlendModeNormal);
CGContextSetRGBStrokeColor(UIGraphicsGetCurrentContext(), red, green, blue, alpha);
}
CGContextSetLineCap(context, kCGLineCapRound);
CGContextSetLineWidth(context, penWidth);
CGContextSetShouldAntialias(context, false);
CGContextSetAllowsAntialiasing(context, false);
if (self.drawDashedLines)
{
CGFloat dashLengths[] = {10.0f, 10.0f};
CGContextSetLineDash(context, 0.0f, dashLengths, 2);
}
CGContextStrokePath(context);
}
}
-(void)drawLinesInContext:(CGContextRef)context
{
对于(int i=0;i<[self.points count];i++)
{
CGPoint movePoint=[[self.movePoints objectAtIndex:i]CGPointValue];
CGPoint controlPoint=[[self.controlPoints objectAtIndex:i]CGPointValue];
CGPoint point=[[self.points objectAtIndex:i]CGPointValue];
CGContextMoveToPoint(上下文,movePoint.x,movePoint.y);
CGContextAddQuadCurveToPoint(上下文,controlPoint.x,controlPoint.y,point.x,point.y);
NSNumber*colorHash=[self.colorHashes objectAtIndex:i];
UIColor*col=[self.colorsForHashValue objectForKey:colorHash];
CGFloat红色=0.0f,蓝色=0.0f,绿色=0.0f,阿尔法=1.0f;
[col getRed:&red-green:&green-blue:&blue-alpha:&alpha];
如果(α=0.0f)
{
笔宽=15.0f;
CGContextSetBlendMode(UIGraphicsGetCurrentContext(),kCGBlendModeClear);
}
其他的
{
笔宽=5.0f;
CGContextSetBlendMode(UIGraphicsGetCurrentContext(),kCGBlendModeNormal);
CGContextSetRGBStrokeColor(UIGraphicsGetCurrentContext(),红色、绿色、蓝色、alpha);
}
CGContextSetLineCap(上下文,kCGLineCapRound);
CGContextSetLineWidth(上下文,笔宽);
CGContextSetShouldAntialias(上下文,false);
CGContextSetAllowsAntialiasing(上下文,false);
if(自绘制虚线)
{
CGFloat dashlength[]={10.0f,10.0f};
CGContextSetLineDash(上下文,0.0f,dashLength,2);
}
CGContextStrokePath(上下文);
}
}
drawLinesContext:
在drawRect:
中调用
由于缓慢拖动时不会出现破折号,我做错了什么
我已经看过了,并尝试了
CGPathAddLineToPoint
以查看它是否有区别,但没有。我遇到了完全相同的问题。问题是在每个后续点之间绘制单独的路径,并且
每条路径都以新的破折号模式开始。如果手指移动缓慢,则绘制
很多很短的路。如果路径短于10点(第一个
绘制的破折号长度)则根本看不到破折号图案
下面的伪代码有望说明如何解决这个问题。而不是
for i = 1 ... N {
move to point[i-1]
curve to point[i]
set line dash
stroke path
}
应该是
move to point[0]
for i = 1 ... N {
curve to point[i]
}
set line dash
stroke path
在我看来,这和这个问题是一样的:。是的,但这是关于
UIBezierPath
,我没有使用:-)我目前无法测试它,但问题似乎是一样的:你画了N条单独的曲线,每条曲线开始一个新的破折号。如果手指移动缓慢,你有许多非常短的路径,因此看不到破折号。-您必须构建并绘制一条连接所有点的路径。你能展示一下你用CGPathAddLineToPoint做了什么吗?这很有意义,但是当我使用CGContextStrokePath
时如何访问当前路径呢?我已尝试将CGContextAddQuadCurveToPoint
替换为CGContextAddLineToPoint(context,point.x,point.y)
,但这没有什么区别。谢谢!这正是我刚才想出来的。当用户举起手指并再次开始画画时,我才移动了这个点。最后一行以CGContextStrokePath
结束,以便能够在相同的点集合中生成不同的颜色。谢谢!