UIView 遮蓋層,中間部分割槽域透明可視
最近公司專案有個功能需要做個遮蓋效果,查了一些資料,總結了兩種方法。
效果如下圖所示:
第一張圖的方法:
新建testview
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;
在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
[[UIColor clearColor] setFill];
UIRectFill( holeRectIntersection );
}
方法二:
CGRect myRect =CGRectMake(100,100,200, 200);
int radius = myRect.size.width/2.0;
UIBezierPath *path = [UIBezierPathbezierPathWithRoundedRect:CGRectMake(0,0, backView.
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];
希望能幫助各位~ ^_^*