1. 程式人生 > >UIView中空遮蓋層,中間部分割槽域透明可視

UIView中空遮蓋層,中間部分割槽域透明可視

方法一:

新建testView

- (void)drawRect:(CGRect)rect {
    // Start by filling the area with the blue color

    [[UIColor colorWithWhite:0.0f alpha:0.5f] setFill];//陰影效果 根據透明度來設計
    UIRectFill( rect );
    CGRect holeRectIntersection = CGRectIntersection( holeRect, rect );
    [[UIColor clearColor] setFill];
    UIRectFill( holeRectIntersection );
}

ViewController中

UIImage *image = [UIImageimageNamed:@"photo3.jpg"];
    [self.view.layersetContents:(id)[imageCGImage]];

    testView *testVW = [[testViewalloc]initWithFrame:self.view.frame];
    testVW.backgroundColor = [UIColorclearColor];
    testVW.opaque =NO;
    [self.viewaddSubview:testVW];

view 的背景色一定要設定成clearColor,opaue一定要設定no;

方法二:

CGRect myRect =CGRectMake(100,100,200, 200);
    int radius = myRect.size.width/2.0;
    UIBezierPath *path = [UIBezierPathbezierPathWithRoundedRect:CGRectMake(0,0, backView.bounds.size.width, backView.bounds.size.height)cornerRadius:0];
    UIBezierPath *circlePath = [UIBezierPathbezierPathWithRoundedRect:CGRectMake(100
,100,2.0*radius,2.0*radius)cornerRadius:radius]; [path appendPath:circlePath]; [path setUsesEvenOddFillRule:YES]; CAShapeLayer *fillLayer = [CAShapeLayerlayer]; fillLayer.path = path.CGPath; fillLayer.fillRule =kCAFillRuleEvenOdd; fillLayer.fillColor = [UIColorgrayColor].CGColor; fillLayer.opacity =0.5; [backView.layeraddSublayer:fillLayer];