【问题标题】:Drop shadow in iosios中的阴影
【发布时间】:2016-10-20 13:11:15
【问题描述】:

如何在 iOS 中放置物体阴影?

我的对象是 UIImageView,我想投下一个椭圆阴影。请参考图片以供参考。

【问题讨论】:

  • 为什么不使用另一个几乎透明的黑色椭圆的图像?
  • 你的对象?你是如何面向对象的。到目前为止,您尝试过什么?
  • @Leo Natan 红色矩形是我的对象。实际上它的 imageview 和我想要 imageview 的阴影,如图所示
  • 我的回答对你没有用吗?可能是你取消接受。

标签: ios objective-c shadow roundedcorners-dropshadow


【解决方案1】:

最好使用其他图像来显示阴影。使用模糊图像或更改图像视图的 alpha。

或者,如果您想以编程方式进行,请尝试:

对象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;

斯威夫特:

//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

输出:

取自http://www.innofied.com/implementing-shadow-ios/,也可以看看了解更多:UIView with rounded corners and drop shadow?

【讨论】:

    【解决方案2】:

    您可以像这样使用 CAShapeLayer:

    目标-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];
    

    斯威夫特 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)
    

    输出:

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-06-12
      • 1970-01-01
      相关资源
      最近更新 更多