公告
财富商城
积分规则
提问
发文
2019-03-05 14:20发布
迷人小祖宗
How to drop an object shadow in iOS?
My object is UIImageView and i want to drop an elliptical shadow.Please refer image for reference.
You can use CAShapeLayer like this:
Objective-C:
// init CAShapeLayer CAShapeLayer *shadowLayer = [CAShapeLayer layer]; shadowLayer.frame = CGRectMake(CGRectGetMinX(_imageView.frame), CGRectGetMaxY(_imageView.frame), _imageView.frame.size.width, _imageView.frame.size.height); shadowLayer.path = [UIBezierPath bezierPathWithOvalInRect:shadowLayer.bounds].CGPath; shadowLayer.fillColor = [UIColor colorWithWhite:0 alpha:0.02].CGColor; shadowLayer.lineWidth = 0; [self.view.layer addSublayer: shadowLayer];
Swift 3
let shadowLayer = CAShapeLayer() shadowLayer.frame = CGRect(x: imageView.frame.minX, y: imageView.frame.maxY, width: imageView.frame.width, height: imageView.frame.height * 0.25) shadowLayer.path = UIBezierPath(ovalIn: shadowLayer.bounds).cgPath shadowLayer.fillColor = UIColor(white: 0, alpha: 0.02).cgColor shadowLayer.lineWidth = 0 view.layer.addSublayer(shadowLayer)
Output:
Better you use another image for showing shadow. Use blur image or change alpha of the imageview.
Or if you want to do it programmatically, try it:
Obj c:
//create elliptical shadow for image through UIBezierPath CGRect ovalRect = CGRectMake(0.0f, _imageView.frame.size.height + 10, _imageView.frame.size.width, 15); UIBezierPath *path = [UIBezierPath bezierPathWithOvalInRect:ovalRect]; //applying shadow to path _imageView.layer.shadowColor = kShadowColor.CGColor; _imageView.layer.shadowOffset = CGSizeMake(0.0, 0.0); _imageView.layer.shadowOpacity = 1.0; _imageView.layer.shadowRadius = 3.0; _imageView.layer.shadowPath = path.CGPath;
Swift :
//create elliptical shdow forimage through UIBezierPath var ovalRect = CGRectMake(0.0, imageView.frame.size.height + 10, imageView.frame.size.width, 15) var path = UIBezierPath(ovalInRect: ovalRect) //applying shadow to path imageView.layer.shadowColor = UIColor(white: 0.0, alpha: 0.5).CGColor imageView.layer.shadowOffset = CGSizeMake(0.0, 0.0) imageView.layer.shadowOpacity = 1.0 imageView.layer.shadowRadius = 3.0 imageView.layer.shadowPath = path.CGPath
Taken from http://www.innofied.com/implementing-shadow-ios/ and also have a look to know more: UIView with rounded corners and drop shadow?
最多设置5个标签!
You can use CAShapeLayer like this:
Objective-C:
Swift 3
Output:
Better you use another image for showing shadow. Use blur image or change alpha of the imageview.
Or if you want to do it programmatically, try it:
Obj c:
Swift :
Output:
Taken from http://www.innofied.com/implementing-shadow-ios/ and also have a look to know more: UIView with rounded corners and drop shadow?