New*_*bie 18 shadow calayer uiview uiimageview ios
我想做一个圆圈UIImageView,它的工作原理.以下是我用它做的方式:
[self.pic.layer setMasksToBounds:YES];
[self.pic.layer setCornerRadius:50.0];
Run Code Online (Sandbox Code Playgroud)
我想补充一些阴影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;
Run Code Online (Sandbox Code Playgroud)
Ama*_*mar 43
使用CALayer's shadowPath属性并添加UIBezierPath带圆角矩形
self.pic.layer.shadowPath = [UIBezierPath bezierPathWithRoundedRect:self.pic.frame cornerRadius:50.0].CGPath;
Run Code Online (Sandbox Code Playgroud)
编辑
对于正方形图像视图,此技术不能直接起作用,因为正如您所说,图像视图会回到正方形.原因:您设置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;
Run Code Online (Sandbox Code Playgroud)
结果效果如屏幕截图所示,

希望有所帮助!
没有容器但有背景视图,这是我的 2 美分
作为 swift 2.2 扩展
image?.applyCircleShadow(5, shadowOpacity: 1)
Run Code Online (Sandbox Code Playgroud)
extension UIView {
func applyCircleShadow(shadowRadius: CGFloat = 2,
shadowOpacity: Float = 0.3,
shadowColor: CGColor = UIColor.blackColor().CGColor,
shadowOffset: CGSize = CGSize.zero) {
layer.cornerRadius = frame.size.height / 2
layer.masksToBounds = false
layer.shadowColor = shadowColor
layer.shadowOffset = shadowOffset
layer.shadowRadius = shadowRadius
layer.shadowOpacity = shadowOpacity
}
}
extension UIImageView {
override func applyCircleShadow(shadowRadius: CGFloat = 2,
shadowOpacity: Float = 0.3,
shadowColor: CGColor = UIColor.blackColor().CGColor,
shadowOffset: CGSize = CGSize.zero) {
// Use UIImageView.hashvalue as background view tag (should be unique)
let background: UIView = superview?.viewWithTag(hashValue) ?? UIView()
background.frame = frame
background.backgroundColor = backgroundColor
background.tag = hashValue
background.applyCircleShadow(shadowRadius, shadowOpacity: shadowOpacity, shadowColor: shadowColor, shadowOffset: shadowOffset)
layer.cornerRadius = background.layer.cornerRadius
layer.masksToBounds = true
superview?.insertSubview(background, belowSubview: self)
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
19436 次 |
| 最近记录: |