macos - 使用 kCGBlendModeClear 在 Mac OS X 上实现橡皮擦描边

标签 macos cocoa core-graphics

我使用 kCGBlendModeClear 混合模式来实现橡皮擦工具。它在 ios 中工作正常,但在 OS X 中不起作用。它在 OS X 中使用 kCGBlendModeClear 混合模式创建黑色线条。

这里是示例代码:

  #if TARGET_OS_IPHONE
    CGContextRef context = UIGraphicsGetCurrentContext();
  #else
    CGContextRef context = [[NSGraphicsContext currentContext] graphicsPort];
  #endif

  CGContextSaveGState(context);

  CGContextSetLineWidth(context, 10);
  CGContextSetRGBStrokeColor(context, 255.0, 0.0, 0.0, 1.0);
  CGContextMoveToPoint(context, 100, 100);
  CGContextAddLineToPoint(context, 200, 200);
  CGContextStrokePath(context);

  CGContextSetBlendMode(context, kCGBlendModeClear);
  CGContextMoveToPoint(context, 150, 150);
  CGContextAddLineToPoint(context, 180, 180);
  CGContextStrokePath(context);

  CGContextRestoreGState(context);

iOS:

in iOS

但在 OS X 中:

in OS X

我也尝试更改 View 和 super View 的不透明属性和背景颜色,但没有帮助。

我知道透明颜色实际上是黑色,alpha 为 0,但我想知道为什么 OS X 中会忽略 alpha。

谢谢。

最佳答案

使用图层支持的 NSView,[view setWantsLayer:YES];

关于macos - 使用 kCGBlendModeClear 在 Mac OS X 上实现橡皮擦描边,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14596404/

相关文章:

swift - NSTableView 获取正在使用键盘编辑的单元格

xcode - 在方案选项中为应用程序语言添加更多选项

iphone - 非矩形形状的插图

objective-c - 如果 Apple 建议不要对类型 ID 进行硬编码,如何识别 Core Foundation 对象?

ios - 绘制辐射渐变 : <Error>: CGContextDrawPath: invalid context 0x0

ios - 在 iOS 中绘图时计算控制点

linux - 字体不工作 - Vim Statusline

objective-c - Xcode 4 中的 NSPredicateEditor

iphone - NSMutableArray 内存管理

swift - 是否可以在 ScreenSaverView 中使用 SpriteKit