ios - 带有 CGContextStrokePath 的虚线
<p><p>我正在开发一个用户应该能够用手指画线的应用程序。用户可以启用虚线,但我在尝试这样做时遇到了一个奇怪的问题。如果用户缓慢拖动手指,则会出现一条实线,但当用户快速拖动手指时,则会出现破折号。无论启用什么,我都希望破折号出现。问题如下图所示:
<img src="/image/73U0o.gif" alt="Dashes do not appear when dragging slowly"/> </p>
<p>我正在使用 <code>UIPanGestureRecognizer</code> 收集接触点:</p>
<pre><code>-(void)dragGestureCaptured:(UIPanGestureRecognizer *)gesture
{
NSValue* touchPoint = ];
if(gesture.state == UIGestureRecognizerStateBegan)
{
];
}
else if (gesture.state == UIGestureRecognizerStateEnded)
{
EBLog(@"Dragging ends..");
return;
}
previousPoint2 = previousPoint1;
previousPoint1 = currentPoint;
currentPoint = ;
CGPoint mid1 = midPoint(previousPoint1, previousPoint2);
CGPoint mid2 = midPoint(currentPoint, previousPoint1);
;
];
];
];
;
}
</code></pre>
<p><code>drawingView</code> 使用 <code>controlsPoints</code>、<code>points</code> 和 <code>movePoints</code> 来绘制线条:</p>
<pre><code>- (void)drawLinesInContext:(CGContextRef)context
{
for (int i = 0; i < ; i++)
{
CGPoint movePoint = [ CGPointValue];
CGPoint controlPoint = [ CGPointValue];
CGPoint point = [ CGPointValue];
CGContextMoveToPoint(context, movePoint.x, movePoint.y);
CGContextAddQuadCurveToPoint(context, controlPoint.x, controlPoint.y, point.x, point.y);
NSNumber* colorHash = ;
UIColor* col = ;
CGFloat red = 0.0f, blue = 0.0f, green = 0.0f, alpha = 1.0f;
;
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);
}
}
</code></pre>
<p><code>drawLinesInContext:</code> 在 <code>drawRect:</code> 中调用。</p>
<p>我做错了什么,因为在缓慢拖动时没有出现破折号?</p>
<p>我已经看过 <a href="https://stackoverflow.com/questions/13016816/drawing-a-dashed-line-with-cgcontextsetlinedash" rel="noreferrer noopener nofollow">Drawing a dashed line with CGContextSetLineDash</a>并尝试了 <code>CGPathAddLineToPoint</code> 看看它是否有所作为,但它没有。我遇到了完全相同的问题。</p></p>
<br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
<p><p>问题是您在每个后续点之间绘制单独的路径,并且
每条路径都以新的破折号模式开始。如果手指缓慢移动,则绘制
很多非常短的路径。如果路径短于 10 个点(第一个
<em>绘制</em>的破折号长度)然后根本看不到破折号图案。</p>
<p>下面的伪代码希望能说明如何解决这个问题。而不是</p>
<pre><code>for i = 1 ... N {
move to point
curve to point
set line dash
stroke path
}
</code></pre>
<p>应该是</p>
<pre><code>move to point
for i = 1 ... N {
curve to point
}
set line dash
stroke path
</code></pre></p>
<p style="font-size: 20px;">关于ios - 带有 CGContextStrokePath 的虚线,我们在Stack Overflow上找到一个类似的问题:
<a href="https://stackoverflow.com/questions/25520388/" rel="noreferrer noopener nofollow" style="color: red;">
https://stackoverflow.com/questions/25520388/
</a>
</p>
页:
[1]