给圆添加一个阴影效果

我想作一圆 UIImageView ,和它的工作。下面是我用来使它的方法:

[self.pic.layer setMasksToBounds:YES];
[self.pic.layer setCornerRadius:50.0];

我想添加一些阴影到 UIImageView 。下面的代码不会添加一些阴影到我图像视图,但是,图像视图更改回方形。有人可以给我一些指针来解决这个问题吗?下面是我使用添加阴影的代码:

self.pic.layer.shadowColor = [UIColor purpleColor].CGColor;
self.pic.layer.shadowOffset = CGSizeMake(0, 1);
self.pic.layer.shadowOpacity = 1;
self.pic.layer.shadowRadius = 1.0;
self.pic.clipsToBounds = NO;
解决方法 1:

使用 CALayer shadowPath 属性和添加 UIBezierPath 与圆角矩形

self.pic.layer.shadowPath = [UIBezierPath bezierPathWithRoundedRect:self.pic.frame cornerRadius:50.0].CGPath;

编辑

Square-ish 图像视图此技术不能直接因为,正如你所说,图像视图可以追溯到广场。原因: 您设置 clipsToBounds = NO 显示阴影,移除为圆角半径,剪切位置 imageView 是子视图的 container

解决方法:
在容器的视图中添加您 imageview,然后到此容器应用层阴影。以下是我试过的代码。

[self.imageView.layer setCornerRadius:60.0];
[self.imageView.layer setMasksToBounds:YES];
self.imageView.clipsToBounds = YES;

self.container.backgroundColor = [UIColor clearColor];
self.container.layer.shadowColor = [UIColor blackColor].CGColor;
self.container.layer.shadowOffset = CGSizeMake(5,15);
self.container.layer.shadowOpacity = 0.5;
self.container.layer.shadowRadius = 2.0;
self.container.layer.shadowPath = [UIBezierPath bezierPathWithRoundedRect:self.container.bounds cornerRadius:100.0].CGPath;

由此产生的效果是在屏幕快照中所示

enter image description here

希望有所帮助 !

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值