用一个切出的圆圈掩盖UIView

时间:2014-01-20 11:21:04

标签: ios objective-c uiview core-animation cashapelayer

我创建了一个带有半透明黑色背景的UIView,可以看到主应用的视图。我的目标是在UIView的某处创建一个切出的圆形,在那里看不到半透明的黑色背景,除了圆形区域外,在整个图像上产生黑色遮罩的效果(这是为了突出显示该区域的按钮)。

我设法使用CAShapeLayer来创建一个圆形蒙版,但这有相反的效果(即视图的其余部分是清晰的,并且圆圈内部是半透明的黑色。我是什么我希望保持半透明的黑色外面的所有东西,然后内部要清楚。这是我使用的代码,我将如何以相反的方式使其工作?我的blackMask是半透明的黑色视图,我的buttonCircle是我想要保持清晰的圆圈。

也许我需要以某种方式颠倒路径?

CAShapeLayer *maskLayer = [[CAShapeLayer alloc] init];
CGRect maskRect = buttonCircle.frame;
CGPathRef path = CGPathCreateWithEllipseInRect(maskRect, NULL);
maskLayer.path = path;
CGPathRelease(path);
blackMask.layer.mask = maskLayer;

编辑:现在尝试这个,没有看到任何掩码:

UIView *circularMaskView = [[UIView alloc] initWithFrame:buttonCircle.frame];
circularMaskView.backgroundColor = [UIColor greenColor];
CAShapeLayer *maskLayer = [[CAShapeLayer alloc] init];

CGPathRef path = CGPathCreateWithEllipseInRect(buttonCircle.frame, NULL);
maskLayer.path = path;
CGPathRelease(path);

circularMaskView.layer.mask = maskLayer;

[blackMask addSubview:circularMaskView];

3 个答案:

答案 0 :(得分:18)

所以,这就是我所做的。我创建了一个名为UIView的自定义BlackoutView子类,如下所示:

<强> BlackoutView.h

#import <UIKit/UIKit.h>

@interface BlackoutView : UIView

@property (nonatomic, retain) UIColor *fillColor;
@property (nonatomic, retain) NSArray *framesToCutOut;

@end

<强> BlackoutView.m

#import "BlackoutView.h"

@implementation BlackoutView

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

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

    for (NSValue *value in self.framesToCutOut) {
        CGRect pathRect = [value CGRectValue];
        UIBezierPath *path = [UIBezierPath bezierPathWithOvalInRect:pathRect];
        [path fill];
    }

    CGContextSetBlendMode(context, kCGBlendModeNormal);
}

@end

然后我将其正常实例化,并将属性设置为我想要使用的蒙版的颜色,以及要从蒙版中剪切的帧:

[blackMask setFillColor:[UIColor colorWithWhite:0.0f alpha:0.8f]];
[blackMask setFramesToCutOut:@[[NSValue valueWithCGRect:buttonCircleA.frame],
                               [NSValue valueWithCGRect:buttonCircleB.frame]]];

这可以通过允许我切除椭圆形以外的其他形状来改善,但它对我的目的来说很好,并且很容易适应以后这样做。希望这有助于其他人!

答案 1 :(得分:8)

如果您使用任何黑色图像进行屏蔽

-(void)maskWithImage:(UIImage*)maskImage toImageView:(UIImageView*)imgView{

    CALayer *aMaskLayer=[CALayer layer];
    aMaskLayer.contents=(id)maskImage.CGImage;

    imgView.layer.mask=aMaskLayer;


}

如果您有任何自定义ShapeLayer来屏蔽

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

如果您想用圆圈制作ImageView蒙版

-(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;

}

答案 2 :(得分:4)

以下是我在Swift中转换接受的答案

public class OverlayView: UIView {
let fillColor: UIColor = UIColor.grayColor()
public var framesToCutOut: Array<NSValue> = [NSValue]()

override public func drawRect(rect: CGRect) {
    super.drawRect(rect)

    fillColor.setFill()
    UIRectFill(rect)

    if let context: CGContextRef = UIGraphicsGetCurrentContext() {
        CGContextSetBlendMode(context, .DestinationOut)

        for value in framesToCutOut {
            let pathRect: CGRect = value.CGRectValue()
            let path: UIBezierPath = UIBezierPath(ovalInRect: pathRect)
            path.fill()
        }

        CGContextSetBlendMode(context, .Normal)
    }
}