iphone - 删除 quartz 2d 中上下文中的剪辑

标签 iphone objective-c ios quartz-2d

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

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

效果非常好,如图所示

context clipping

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

enter image description here

如何让我的上下文停止剪辑,以便我可以将该圆圈置于剪辑之外?

我画圆的代码是

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/

相关文章:

objective-c - 在 Linux (Ubuntu) 上编译 Objective-C 项目

ios - UITableView - AFNetworking 后台操作卡住滚动

ios - admob横幅广告在游戏场景中大约一分钟后显示广告忽略bannerAd.isHidden xcode 8

ios - 如何访问其他类中的对象?

iphone - 从远程 sql server 获取数据 - iPhone sdk

iPhone 应用程序启动

ios - 无法在 iOS 中将 XMPPFramework 连接到 Openfire 服务器

ios - 在表格 View 中仅选择一个自定义单元格的文本字段

iphone - 了解 iOS 设备是否具有蜂窝数据功能

iphone - UICollectionView 报纸状布局