UIView的左右阴影

时间:2013-01-08 16:19:53

标签: iphone ios calayer shadow

您好我想在视图中添加一个CALayer Shadow,其中阴影位于视图的右侧,最简单的方式是:

someView.layer.shadowColor = [[UIColor blackColor] CGColor];
someView.layer.shadowOffset = CGSizeMake(0.0f,0.0f);
someView.layer.shadowOpacity = 1.0f;
someView.layer.shadowRadius = 10.0f;
someView.layer.shadowPath = [[UIBezierPath bezierPathWithRect:someView.bounds] CGPath];

但是当我增加shadowRadius它看起来不太好时,我会添加一个更大的阴影就像阴影一样。我如何制作一个左右看起来很好的阴影。

4 个答案:

答案 0 :(得分:29)

我认为10是一个相当大的阴影半径,尝试3或4而不透明,我通常使用0.7:

someView.layer.shadowColor = [[UIColor blackColor] CGColor];
someView.layer.shadowOffset = CGSizeMake(0.0f,0.0f);
someView.layer.shadowOpacity = 0.7f;
someView.layer.shadowRadius = 4.0f;

如果您只想在左右两侧使用阴影,则在顶部和底部插入矩形,以便隐藏顶部和底部阴影:

CGRect shadowRect = CGRectInset(someView.bounds, 0, 4);  // inset top/bottom
someView.layer.shadowPath = [[UIBezierPath bezierPathWithRect:shadowRect] CGPath];

我不确定这是不是你想要的。

答案 1 :(得分:6)

swift 3.0版

imageView.layer.shadowOpacity = 0.8
imageView.layer.shadowOffset = CGSize(width: 0, height: 3)
imageView.layer.shadowRadius = 4.0

let shadowRect: CGRect = imageView.bounds.insetBy(dx: 0, dy: 4)
imageView.layer.shadowPath = UIBezierPath(rect: shadowRect).cgPath

答案 2 :(得分:0)

progrmr的回答非常有帮助,欢呼!

我制作了一个滑出式菜单,我的VC周围有阴影问题,导致导航栏中断。原来我不得不插入阴影层。

这是我使用swift的解决方案:

rightViewController!.view.layer.shadowOpacity = 0.8
rightViewController!.view.layer.shadowOffset = CGSizeMake(0, 3)
rightViewController!.view.layer.shadowRadius = 4.0

let shadowRect: CGRect = CGRectInset(rightViewController!.view.bounds, 0, 4);  // inset top/bottom
rightViewController!.view.layer.shadowPath = UIBezierPath(rect: shadowRect).CGPath

答案 3 :(得分:0)

我创建了一个可以在顶部/底部或左侧/右侧使用的阴影,完全没有偏见。如果您选择顶部/底部等,则左侧/右侧不会出现阴影。我希望我有所帮助。代码如下:


import UIKit

class ViewController: UIViewController {
  
    
     override func viewDidLoad() {
        super.viewDidLoad()
        
        let imgView = UIImageView(frame: CGRect(x: self.view.bounds.midX - 150, y: self.view.bounds.midY - 150, width: 300, height: 300))
            
        imgView.contentMode = .scaleToFill
        imgView.image = UIImage(named: "name image")

        self.view.addSubview(imgView)
        //You can put left/right or top/bottom
        imgView.addShadow(sides: .leftRight, constant: 10, alpha: 5)

        
     }

}

extension UIView{
    
    func addShadow(sides: Sides, constant: Int, alpha: Int){
        if(constant > 0 && alpha > 0){
        switch sides {
            case .leftRight:
   
                let view = UIView(frame: CGRect(x: -CGFloat(constant), y: 0, width: self.bounds.width + CGFloat((constant * 2)), height: self.bounds.height))
                self.addSubview(view)

                addMask(sides: .leftRight, view: view, constant: constant, shadowRadius: alpha)
                
            case .topBottom:

                let view = UIView(frame: CGRect(x: 0, y: -CGFloat(constant), width: self.bounds.width , height: self.bounds.height + CGFloat(constant * 2)))
                self.addSubview(view)

                addMask(sides: .topBottom, view: view, constant: constant, shadowRadius: alpha)

            }
        }
    }
    
    private func addMask(sides:Sides, view: UIView, constant: Int, shadowRadius:Int){
        let mutablePath = CGMutablePath()
        let mask = CAShapeLayer()

        switch sides {
        case .leftRight:
            
            mutablePath.addRect(CGRect(x: -CGFloat(constant), y: 0, width: view.bounds.width, height: view.bounds.height))

            mutablePath.addRect(CGRect(x: CGFloat(constant) , y: 0, width: view.bounds.width , height: view.bounds.height))

            
        case .topBottom:
            
            mutablePath.addRect(CGRect(x: 0, y: -CGFloat(constant), width: view.bounds.width, height: view.bounds.height))

            mutablePath.addRect(CGRect(x: 0, y: CGFloat(constant) , width: view.bounds.width , height: view.bounds.height))
        }

        mask.path = mutablePath

        mask.fillRule = CAShapeLayerFillRule.evenOdd
        mask.fillColor  = UIColor(white:1.0, alpha: 0.2).cgColor
        view.layer.mask = mask
        view.layer.addSublayer(mask)
        mask.shadowColor = UIColor.black.cgColor
        mask.shadowRadius = CGFloat(shadowRadius)
        mask.shadowOpacity = 1.0
        mask.shadowOffset = CGSize(width: 0, height: 0)

    }
}

enum Sides{
    case leftRight
    case topBottom
}

你只需要调用扩展 UIView,它有 addShadow() 函数并传递你想要的参数。

enter image description here

enter image description here