【问题标题】:remove clip on context in quartz 2d在石英 2d 中删除上下文中的剪辑
【发布时间】:2012-09-23 05:51:20
【问题描述】:

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

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);

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

【问题讨论】:

    标签: iphone objective-c ios quartz-2d


    【解决方案1】:

    您可以发出 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);
    

    【讨论】:

    • 太棒了,非常感谢!为了将圆圈移到剪辑区域之外,我只是在将路径添加到上下文之前将 CGContextRestoreGState() 移到了。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-05-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多