UIView 的clipToBounds屬性 和 CALayer的maskToBounds屬性,都可以實(shí)現(xiàn)視圖的四周剪裁森逮,即超出視圖范圍不顯示
但是如果只想超出某一邊的視圖不顯示,另外幾邊的視圖超出部分依舊顯示磁携,這種需求怎么處理呢褒侧?
有一個(gè)比較簡(jiǎn)單,但是并不靈活的方式的
UIView *view = [[UIView alloc] initWithFrame:CGRectMake(50, 100, 200, 200)];
view.clipsToBounds = NO;
view.backgroundColor = [UIColor redColor];
CAShapeLayer *layer = [CAShapeLayer layer];
layer.path = [UIBezierPath bezierPathWithRect:CGRectMake(0, -50, 300, 300)].CGPath;
view.layer.borderWidth = 1;
view.layer.borderColor = [UIColor blackColor].CGColor;
view.layer.mask = layer;
UIView *sub = [[UIView alloc] initWithFrame:CGRectMake(-20, -20, 240, 240)];
sub.backgroundColor = [[UIColor blueColor] colorWithAlphaComponent:0.3];
[view addSubview:sub];
[self.view addSubview:view];
這種方式谊迄,可以使得view左邊被剪裁闷供,超出左邊的部分不顯示,其他幾邊超出的部分依舊會(huì)顯示
只是需要嚴(yán)格計(jì)算超出的部分的大小鳞上,才能達(dá)到比較理想的效果