Mask a UIView with a cut-out circle

前端 未结 3 1022
南旧
南旧 2020-12-13 21:09

I’ve created a UIView with a semi-transparent black background to sit across my main app’s view. What I’m aiming for here is to create a cut-out circle shape so

3条回答
  •  -上瘾入骨i
    2020-12-13 21:22

    If You are using any Black Image to mask then

    -(void)maskWithImage:(UIImage*)maskImage toImageView:(UIImageView*)imgView{
    
        CALayer *aMaskLayer=[CALayer layer];
        aMaskLayer.contents=(id)maskImage.CGImage;
    
        imgView.layer.mask=aMaskLayer;
    
    
    }
    

    If you have any Custom ShapeLayer to mask

    -(void)maskWithShapeLayer:(CALayer*)layer toImageView:(UIImageView*)imgView{
        //Layer should be filled with Black color to mask those part of Image
        imgView.layer.mask=layer;
    }
    

    If You want to make ImageView Mask with Circle

    -(void)maskWithCircle:(UIImageView*)imgView{
    
        CAShapeLayer *aCircle=[CAShapeLayer layer];
        aCircle.path=[UIBezierPath bezierPathWithRoundedRect:imgView.bounds cornerRadius:imgView.frame.size.height/2].CGPath; // Considering the ImageView is square in Shape
    
        aCircle.fillColor=[UIColor blackColor].CGColor;
        imgView.layer.mask=aCircle;
    
    }
    

提交回复
热议问题