2

我用弧线绘制了一条到我的上下文的路径,将其设置为剪辑,并将图像绘制到该剪辑中。

CGContextBeginPath(context);
CGContextMoveToPoint(context, lineStartPoint.x, lineStartPoint.y);
CGContextAddArc(context, lineStartPoint.x, lineStartPoint.y, 105, toAngle*M_PI,fromAngle*M_PI , 1);
CGContextMoveToPoint(context, toLinePoint.x, toLinePoint.y);
CGContextClip(context);
[[UIImage imageNamed:@"gradientfill.png"] drawInRect:[self bounds]];

如此完美,如图所示

上下文剪辑

我的问题是,在那个渐变的底部,我画了一个我想存在于剪辑之外的圆圈,它应该看起来像这样

在此处输入图像描述

如何让我的上下文停止剪辑,以便我可以在剪辑之外拥有那个圆圈?

我画圆的代码是

CGMutablePathRef path = CGPathCreateMutable();
CGRect toHandleRect = CGRectMake(toLinePoint.x-5, toLinePoint.y-5, 10, 10);    
CGPathAddEllipseInRect(path, NULL, toHandleRect);
CGContextAddPath(context, path);
CGPathRelease(path);

我希望用户能够将小圆圈拖到该视图中的任何位置。

4

1 回答 1

5

您可以发出 CGContextSaveGState + CGContextRestoreGState 对,如下例所示:

// Saves the context including the current clipping region.
CGContextSaveGState(context);

// Build you clip region and do some drawing
CGContextBeginPath(context);
CGContextMoveToPoint(context, lineStartPoint.x, lineStartPoint.y);
CGContextAddArc(context, lineStartPoint.x, lineStartPoint.y, 105, toAngle*M_PI,fromAngle*M_PI , 1);
CGContextMoveToPoint(context, toLinePoint.x, toLinePoint.y);
CGContextClip(context);

[[UIImage imageNamed:@"gradientfill.png"] drawInRect:[self bounds]];

CGMutablePathRef path = CGPathCreateMutable();
CGRect toHandleRect = CGRectMake(toLinePoint.x-5, toLinePoint.y-5, 10, 10);    
CGPathAddEllipseInRect(path, NULL, toHandleRect);
CGContextAddPath(context, path);
CGPathRelease(path);

// Restore the original state
CGContextRestoreGState(context);
于 2012-09-23T06:47:01.487 回答