我用弧线绘制了一条通往上下文的路径,将其设置为剪辑,并将图像绘制到该剪辑中。
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);
我希望用户能够将小圆圈拖动到该 View 中的任何位置。
最佳答案
您可以发出 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);
关于iphone - 删除 quartz 2d 中上下文中的剪辑,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12550078/