如何获取UIBezierPath的反向路径

Shu*_*ank 13 iphone objective-c ios uibezierpath

self.myPath=[UIBezierPath bezierPathWithArcCenter:center 
                                           radius:200 
                                       startAngle:0 
                                         endAngle:180 
                                        clockwise:YES];
Run Code Online (Sandbox Code Playgroud)

(这让我能够通过一些网络搜索来运行起来).

我有这条路.现在我想填补这条路径的反面,所以留下这部分并填充其他所有内容.我怎样才能完成编码?我对此没有太多信息.

问题

在此输入图像描述

在使用Cemal之后它显示的区域之前它只显示了一个带红色笔划的圆圈.

编辑

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        self.backgroundColor = [UIColor whiteColor];
        self.punchedOutPath =  
         [UIBezierPath bezierPathWithOvalInRect:CGRectMake(50, 50, 400, 400)];
        self.fillColor = [UIColor redColor];
        self.alpha = 0.8;
    }
    return self;
}

- (void)drawRect:(CGRect)rect
{
    [[self fillColor] set];
    UIRectFill(rect);

    CGContextRef ctx = UIGraphicsGetCurrentContext();
    CGContextSetBlendMode(ctx, kCGBlendModeDestinationOut);

    [[self punchedOutPath] fill];

    CGContextSetBlendMode(ctx, kCGBlendModeNormal);
}
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

ada*_*ulf 21

使用bezierPathByReversingPath.来自文档(仅限iOS 6.0+):

使用当前路径的反转内容创建并返回一个新的贝塞尔路径对象.

所以要改变你的道路,你只需要:

UIBezierPath* aPath = [UIBezierPath bezierPathWithArcCenter:center
                                                     radius:200
                                                 startAngle:0
                                                   endAngle:180
                                                  clockwise:YES];
self.myPath = [aPath bezierPathByReversingPath];
Run Code Online (Sandbox Code Playgroud)


Dav*_*ong 13

这是一个不需要反转路径的替代方案.

您有一部分视图,您基本上想要"剪辑":

剪掉了

假设您希望白色区域[UIColor whiteColor]具有75%的alpha值.以下是您快速完成的方法:

  • 您创建一个新的UIView子类.
  • 该视图有两个属性:

    @property (retain) UIColor *fillColor;
    @property (retain) UIBezierPath *punchedOutPath;
    
    Run Code Online (Sandbox Code Playgroud)
  • 您重写其-drawRect:方法来执行此操作:

    - (void)drawRect:(CGRect)rect {
      [[self fillColor] set];
      UIRectFill(rect);
    
      CGContextRef ctx = UIGraphicsGetCurrentContext();
      CGContextSetBlendMode(ctx, kCGBlendModeDestinationOut);
    
      [[self punchedOutPath] fill];
    
      CGContextSetBlendMode(ctx, kCGBlendModeNormal);
    }
    
    Run Code Online (Sandbox Code Playgroud)

这里有一个警告:fillColor视图的视图不能包含alpha组件.所以在你的情况下,你想要的只是[UIColor whiteColor].然后通过调用自己应用alpha位[myView setAlpha:0.75].

这里发生了什么:这是使用称为"目的地输出"的混合模式.在数学上它被定义为R = D*(1 - Sa),但在外行的术语中,它意味着"目的地图像在哪里目标图像是不透明的,但源图像是透明的,在其他地方是透明的".

所以它将使用目标(即,已经在上下文中的内容),无论东西是透明的(即贝塞尔路径之外),然后贝塞尔路径将是不透明的,那么这些东西将变得透明.但是,目标内容必须已经是不透明的.如果它不是不透明的,那么混合不会做你想要的.这就是为什么你必须提供一个不透明的UIColor,然后直接对视图做任何你需要的透明度.


在这些情况下,我自己运行了这个:

  • 窗口有[UIColor greenColor]背景
  • fillColor是白色的
  • punchedOutPath是一个椭圆形,从视图的边缘插入10个点.
  • 视图具有alpha0.75

使用上面的代码,我得到了这个:

在此输入图像描述

内部为纯绿色,外部为半透明覆盖层.


更新

如果您的封面是图像,那么您需要创建一个新图像.但原则是一样的:

UIImage* ImageByPunchingPathOutOfImage(UIImage *image, UIBezierPath *path) {
  UIGraphicsBeginImageContextWithOptions([image size], YES, [image scale]);

  [image drawAtPoint:CGPointZero];

  CGContextRef ctx = UIGraphicsGetCurrentContext();
  CGContextSetBlendMode(ctx, kCGBlendModeDestinationOut);
  [path fill];


  UIImage *final = UIGraphicsGetImageFromCurrentImageContext();
  UIGraphicsEndImageContext();
  return final;
}
Run Code Online (Sandbox Code Playgroud)

然后,您将获取此函数的结果并将其放入UIImageView.


Cem*_*ker 2

我有两个解决方案给你。

  1. 在 上画出这条路径CALayer。并将其用作CALayer实际的遮罩层CALayer

  2. 在添加圆弧之前,先根据框架的大小绘制一个矩形。

    UIBezierPath *path = [UIBezierPath bezierPathWithRect:view.frame];
    [path addArcWithCenter:center 
                    radius:200 
                startAngle:0 
                  endAngle:2*M_PI 
                 clockwise:YES];
    
    Run Code Online (Sandbox Code Playgroud)

我会使用第二种解决方案。:)

  • 第二种解决方案更好,只是需要逆时针添加弧线,因此它划定的区域被[非零绕数规则](https://developer.apple.com/library/ios/documentation/graphicsimaging/ Conceptual/drawingwithquartz2d/dq_paths/dq_paths.html#//apple_ref/doc/uid/TP30001066-CH211-TPXREF106)(矩形按顺时针方向添加)。 (2认同)