Create layer mask with custom-shaped hole

前端 未结 1 1615
青春惊慌失措
青春惊慌失措 2020-12-14 03:33

I\'ve spent too much time trying to figure this out and simply cannot find a workable solution.

Situation: 1. A picture of \'something\' is displaye

相关标签:
1条回答
  • 2020-12-14 04:18

    I came back to this later after learning other core graphics techniques. The solution is closest to the Multiple Layer Mask Example above. However, instead of creating an inner and outer layer, you need to combine two paths into a single UIBezierPath in opposite directions.

    So, e.g., create a path of the inner area to be cropped (CW). NOTE: x,y,w,h are referring to the origin and size of the "hole".

          [path moveToPoint:ccp(x,y)];
          [path addLineToPoint:ccp(x+w,y)];
          [path addLineToPoint:ccp(x+w,y+h)];
          [path addLineToPoint:ccp(x,y+h)];
          [path addLineToPoint:ccp(x,y)];
    

    Then, add to the same path the outer area in the opposite direction (CCW). NOTE: x,y,w,h are referring to the origin and size of the outer rect.

          [path moveToPoint:ccp(x,y)];
          [path addLineToPoint:ccp(x,y+h)];
          [path addLineToPoint:ccp(x+w,y+h)];
          [path addLineToPoint:ccp(x+w,y)];
          [path addLineToPoint:ccp(x,y)];
    

    This path is then applied to a layer (maskLayer), which is used as the mask on the final layer (colorLayer). The "outerLayer" is not needed.

    0 讨论(0)
提交回复
热议问题