UIView与阴影的圆angular

我正在尝试显示带有圆angular和投影的UIView。 但问题是,maskToBounds属性只适用于任何一种情况。

如果maskToBounds为YES,则会出现圆angular,如果没有,则会出现阴影。 这是实现,但它只显示没有阴影的圆angular:

[self.view setBackgroundColor:[UIColor colorWithPatternImage:[UIImage imageNamed:@"blue_gradient.jpeg"]]]; self.view.layer.masksToBounds = YES; self.view.layer.opaque = NO; self.view.layer.cornerRadius = 15.0f; self.view.layer.shadowColor = [UIColor blackColor].CGColor; self.view.layer.shadowRadius = 5.0; self.view.layer.shadowOffset = CGSizeMake(3.0, 3.0); self.view.layer.shadowOpacity = 0.9f; 

想法!

注意:我已经阅读并实现了以下线程中的代码,但它不起作用: 具有圆angular和阴影的UIView?

更新1:

我试图创build两个单独的视图。 一个将代表半径,一个代表阴影。 问题是,如下面的截图所示,在半径视图的顶部创build阴影:

在这里输入图像说明

H

 ere is the code: self.view.layer.masksToBounds = YES; self.view.layer.opaque = NO; self.view.layer.cornerRadius = 15.0f; // self.view.backgroundColor = [UIColor clearColor]; self.view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"blue_gradient.jpeg"]]; // UIView *shadowView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 100, 100)]; shadowView.layer.shadowColor = [UIColor blackColor].CGColor; shadowView.layer.shadowRadius = 2.0; shadowView.backgroundColor = [UIColor clearColor]; shadowView.layer.shadowOffset = CGSizeMake(3.0, 3.0); shadowView.layer.shadowOpacity = 0.9f; shadowView.layer.shadowPath = [UIBezierPath bezierPathWithRect:CGRectMake(0, 0, 100, 100)].CGPath; [self.view addSubview:shadowView]; 

更新2:

倒置仍然不起作用。 没有创build圆angular。

 UIView *roundCornerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 100, 100)]; roundCornerView.layer.masksToBounds = YES; roundCornerView.layer.opaque = NO; roundCornerView.layer.cornerRadius = 15.0f; self.view.layer.shadowColor = [UIColor blackColor].CGColor; self.view.layer.shadowRadius = 2.0; //self.view.backgroundColor = [UIColor clearColor]; self.view.layer.shadowOffset = CGSizeMake(3.0, 3.0); self.view.layer.shadowOpacity = 0.9f; self.view.layer.shadowPath = [UIBezierPath bezierPathWithRect:CGRectMake(0, 0, 100, 100)].CGPath; [self.view addSubview:roundCornerView]; 

解:

  UIView *roundCornerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 100, 100)]; roundCornerView.layer.masksToBounds = YES; roundCornerView.layer.opaque = NO; roundCornerView.layer.cornerRadius = 15.0f; roundCornerView.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"blue_gradient.jpeg"]]; self.view.layer.shadowColor = [UIColor blackColor].CGColor; self.view.layer.shadowRadius = 2.0; self.view.backgroundColor = [UIColor clearColor]; self.view.layer.shadowOffset = CGSizeMake(3.0, 3.0); self.view.layer.shadowOpacity = 0.9f; //self.view.layer.shadowPath = [UIBezierPath // bezierPathWithRect:CGRectMake(0, 0, 100, 100)].CGPath; [self.view addSubview:roundCornerView]; 

创build两个视图。 一个带有阴影(并且不要忘记设置shadowPath),在这个阴影中你添加了cornerRadiusmaskToBounds子视图。

接受的答案不包括任何代码,所以这里是一个在Swift中的例子(请参阅Obj-C中OP解决scheme的原始问题)。

在这里输入图像说明

就像接受的答案一样,这个解决scheme使用单独的视图来绘制阴影和angular半径。

 // add the shadow to the base view baseView.backgroundColor = UIColor.clear baseView.layer.shadowColor = UIColor.black.cgColor baseView.layer.shadowOffset = CGSize(width: 3, height: 3) baseView.layer.shadowOpacity = 0.7 baseView.layer.shadowRadius = 4.0 // improve performance baseView.layer.shadowPath = UIBezierPath(roundedRect: baseView.bounds, cornerRadius: 10).cgPath baseView.layer.shouldRasterize = true baseView.layer.rasterizationScale = UIScreen.main.scale // add the border to subview let borderView = UIView() borderView.frame = baseView.bounds borderView.layer.cornerRadius = 10 borderView.layer.borderColor = UIColor.black.cgColor borderView.layer.borderWidth = 1.0 borderView.layer.masksToBounds = true baseView.addSubview(borderView) // add any other subcontent that you want clipped let otherSubContent = UIImageView() otherSubContent.image = UIImage(named: "lion") otherSubContent.frame = borderView.bounds borderView.addSubview(otherSubContent) 

我的完整答案在这里 。