相关疑难解决方法(0)

为什么maskToBounds = YES会阻止CALayer阴影?

使用以下代码片段,我将向我的UIView添加阴影效果.哪个效果很好.但是只要我将视图的masksToBounds属性设置为YES即可.投影效果不再呈现.

self.myView.layer.shadowColor = [[UIColor blackColor] CGColor];
self.myView.layer.shadowOpacity = 1.0;
self.myView.layer.shadowRadius = 10.0;
self.myView.layer.shadowOffset = CGSizeMake(0.0f, 0.0f);
self.myView.layer.cornerRadius = 5.0;
self.myView.layer.masksToBounds = YES; // <-- This is causing the Drop shadow to not be rendered
UIBezierPath *path = [UIBezierPath bezierPathWithCurvedShadowForRect:self.myView.bounds];
self.myView.layer.shadowPath = path.CGPath;
self.myView.layer.shouldRasterize = YES;
Run Code Online (Sandbox Code Playgroud)

你有什么想法吗?

objective-c calayer uiview ios

81
推荐指数
3
解决办法
5万
查看次数

UIView圆角与阴影

我试图展示一个带圆角和投影的UIView.但问题是maskToBounds属性仅适用于任何一种情况.

如果maskToBounds为YES,则显示圆角,当为NO时,则显示阴影.这是实现,但它只显示没有阴影的圆角:

[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;
Run Code Online (Sandbox Code Playgroud)

想法!

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

更新1:

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

在此输入图像描述

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 …
Run Code Online (Sandbox Code Playgroud)

ios

15
推荐指数
2
解决办法
1万
查看次数

标签 统计

ios ×2

calayer ×1

objective-c ×1

uiview ×1