首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >UIView左侧和右侧的阴影

UIView左侧和右侧的阴影
EN

Stack Overflow用户
提问于 2013-01-09 00:19:53
回答 4查看 22.4K关注 0票数 6

您好,我喜欢添加一个CALayer阴影到一个视图,其中的阴影是视图的左右,最简单的方法是:

代码语言:javascript
复制
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时,它看起来不太好。我怎样才能使阴影在左右两边看起来都很好。

EN

Stack Overflow用户

发布于 2021-05-06 02:24:26

我创建了一个阴影,可以在顶部/底部或左侧/右侧使用,完全没有偏见。如果选择顶部/底部,则不会在左侧/右侧显示阴影,依此类推。我希望我能帮上忙。代码如下:

代码语言:javascript
复制
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()函数并传递所需的参数。

票数 0
EN
查看全部 4 条回答
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/14219412

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档