iphone - iOS 中的外部填充颜色

标签 iphone ios cgpath cashapelayer

我有两个矩形(两个闭合子 cgpaths)。 矩形 B 很小,并且存在于矩形 A 内部。其中的所有边。 有没有一种直接的方法来填充矩形 B 外部的颜色区域。

CAShapeLayer fillExternalColor 类似的东西?如果不是直接方式,如何以编程方式执行此操作?

A - 紫色 B - 黄色

Drawing A and then drawing B Drawing B and then drawing A

所以我尝试先画 A,然后再画 B。我希望 B 的颜色清晰(现在我放黄色),但后来我看到 A 的颜色是紫色..

我找到了提供 AnB 的 CGRectIntersection 方法和提供 AuB 的 CGRectUnion 方法。 有没有一种方法可以给出 AuB - AnB 的其余区域?

最佳答案

您正在使用 CAShapeLayer。如果您的 fillColorstrokeColor 都是不透明的(alpha 1.0),您只需设置图层的 backgroundColor 来填充那些位于层的边界,但在其描边和填充路径之外。

我的测试代码:

@implementation ViewController {
    CAShapeLayer *layer;
}

- (void)viewDidLoad {
    [super viewDidLoad];

    layer = [CAShapeLayer layer];
    layer.path = [UIBezierPath bezierPathWithOvalInRect:CGRectMake(50, 50, 250, 250)].CGPath;
    layer.fillColor = [UIColor yellowColor].CGColor;
    layer.strokeColor = [UIColor whiteColor].CGColor;
    layer.lineWidth = 4;
    layer.backgroundColor = [UIColor purpleColor].CGColor;
    [self.view.layer addSublayer:layer];
}

- (void)viewDidLayoutSubviews {
    layer.frame = self.view.bounds;
}

@end

结果:

screen shot of test app

关于iphone - iOS 中的外部填充颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16572335/

相关文章:

ios - UIView 动画使 UICollectionView 委托(delegate)变得模糊 - Swift

ios - 如何在ios中获取paypal(1.4.3)的交易ID?

ios - 由于 es6-prepare-plugin,安装插件总是失败

ios - .avi 文件视频无法在 MPMoviePlayerController ios 播放器中播放

swift - 通过 transform.scale 属性动画化 CAShapeLayer 的路径

cocoa - 如何获取 Quartz 用于描边 NSBezierPath 的路径

iphone - Shadowbox YouTube 视频未出现在 iphone 设备上

ios - 邀请用户使用Facebook安装iOS应用

swift - 为什么 'nil' 与 Swift 3 中的 'UnsafePointer<CGAffineTransform>' 不兼容?

iphone - 核心数据实体继承 --> 限制?