Simply mask a UIView with a rectangle

前端 未结 7 1485
别跟我提以往
别跟我提以往 2020-11-30 20:10

I want to know how to simply mask the visible area of a UIView of any kind. All the answers/tutorials I\'ve read so far describe masking with an image, gradient or creating

7条回答
  •  时光说笑
    2020-11-30 20:33

    Thanks to the link from MSK, this is the way I went with which works well:

    // Create a mask layer and the frame to determine what will be visible in the view.
    CAShapeLayer *maskLayer = [[CAShapeLayer alloc] init];
    CGRect maskRect = CGRectMake(0, 0, 50, 100);
    
    // Create a path with the rectangle in it.
    CGPathRef path = CGPathCreateWithRect(maskRect, NULL);
    
    // Set the path to the mask layer.
    maskLayer.path = path;
    
    // Release the path since it's not covered by ARC.
    CGPathRelease(path);
    
    // Set the mask of the view.
    viewToMask.layer.mask = maskLayer;
    

提交回复
热议问题