22

I do not understand how to remove a shadow that was added to a view. I add to my view in initWithFrame a shadow in this way:

self.layer.borderWidth = 2;
self.layer.borderColor = [UIColor clearColor].CGColor;
self.backgroundColor = [UIColor greenColor];
[self.layer setCornerRadius:8.0f];
CALayer *layer = self.layer;
layer.shadowOffset = CGSizeMake(2, 2);
layer.shadowColor = [[UIColor blackColor] CGColor];
layer.cornerRadius = 8.0f;
layer.shadowRadius = 3.0f;
layer.shadowOpacity = 0.80f;
layer.shadowPath = [[UIBezierPath bezierPathWithRect:layer.bounds] CGPath];

After in the execution of the app I want to remove the shadow from this view. I've tried using:

layer.hidden = YES;

or

self.layer.hidden = YES;

but this hides the view completely, not just the added shadow.

Is there a way to retrieve the added shadow from a view and then hide it? Thanks!

4

7 回答 7

53

我想你可以使用shadowOpacity你的CALayer.

所以这应该工作:

self.layer.shadowOpacity = 0;

请参阅CALayershadowOpacity文档页面

并显示你的影子使用:

self.layer.shadowOpacity = 1.0;
于 2013-05-25T15:51:19.657 回答
7

抱歉,不确定正确的方法,但是您是否尝试过更改 的属性layer shadow?例如,其中之一;

 layer.shadowOffset = CGSizeMake(0, 0);
 layer.shadowColor = [[UIColor clearColor] CGColor];
 layer.cornerRadius = 0.0f;
 layer.shadowRadius = 0.0f;
 layer.shadowOpacity = 0.00f;
于 2013-05-25T15:52:18.930 回答
6

斯威夫特 4.2

我在标签和导航栏的代码中使用它。

extension UIView {

    func shadow(_ height: Int = 5) {
        self.layer.masksToBounds = false
        self.layer.shadowRadius = 4
        self.layer.shadowOpacity = 1
        self.layer.shadowColor = UIColor.gray.cgColor
        self.layer.shadowOffset = CGSize(width: 0 , height: height)
    }

    func removeShadow() {
        self.layer.shadowOffset = CGSize(width: 0 , height: 0)
        self.layer.shadowColor = UIColor.clear.cgColor
        self.layer.cornerRadius = 0.0
        self.layer.shadowRadius = 0.0
        self.layer.shadowOpacity = 0.0
    }
}
于 2019-01-21T16:48:17.733 回答
2

我尝试了其他答案,唯一对我有用的是切换layer.masksToBoundstrue/false

lazy var myView: UIView = {
    let view = UIView()
    view.translatesAutoresizingMaskIntoConstraints = false
    view.backgroundColor = .white
    view.layer.cornerRadius = 5
    view.layer.shadowColor = UIColor.black.cgColor
    view.layer.shadowOpacity = 3
    view.layer.shadowOffset = .zero
    view.layer.shadowRadius = 5
    return view
}()

func showShadow() {

    myView.layer.masksToBounds = false
}


func hideShadow() {

    myView.layer.masksToBounds = true
}
于 2020-06-18T21:28:31.747 回答
1

您试图隐藏的“层”是您对其有阴影的对象的层,它不是可见的方面..只有层中的对象......无论如何,概念化相当混乱,唯一的删除阴影的方法是撤消您最初所做的,这是上面建议的,没有定义的属性,您可以切换一个布尔值并使阴影消失

于 2013-05-25T16:20:54.600 回答
0

斯威夫特 4.2

只是为了添加其他答案,您可以通过动画更改不透明度:

@IBAction func btnDidTap(_ sender: UICustomGestureRecognizer) {
        guard let view = sender.view else {return}
        if sender.state == .began {
            UIView.animate(withDuration: 0.3,
                           animations: {
                view.layer.shadowOpacity = 0
            })
            
        } else if sender.state == .ended {
            UIView.animate(withDuration: 0.3,
                           animations: {
                view.layer.shadowOpacity = 1
            })
        }
}
于 2022-01-06T10:32:53.277 回答
0

斯威夫特 5.+

我的解决方案是添加一个shadowBackgroundView,它有一个可移动的shadowLayer. 通过这种方式,我可以轻松移除图层而无需重置阴影属性。

class ViewController: UIViewController {

    private let shadowBackgroundView: UIView = {
        let view = UIView(frame: CGRect(x: 100, y: 100, width: 100, height: 100))
        view.layer.masksToBounds = false
        return view
    }()

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view.

        view.addSubview(shadowBackgroundView)

        // the view you want to add the shadow
        let dummyView = UIView(frame: CGRect(x: 0, y: 0, width: 100, height: 100))
        dummyView.backgroundColor = .red
        shadowBackgroundView.addSubview(dummyView)

        let addShadowButton = UIButton(frame: CGRect(x: 100, y: 300, width: 140, height: 50))
        addShadowButton.backgroundColor = .blue
        addShadowButton.setTitle("Add Shadow", for: .normal)
        addShadowButton.addTarget(self, action: #selector(addShadow), for: .touchUpInside)

        let removeShadowButton = UIButton(frame: CGRect(x: 100, y: 450, width: 140, height: 50))
        removeShadowButton.backgroundColor = .blue
        removeShadowButton.setTitle("Remove Shadow", for: .normal)
        removeShadowButton.addTarget(self, action: #selector(removeShadow), for: .touchUpInside)

        view.addSubview(addShadowButton)
        view.addSubview(removeShadowButton)
    }

    @objc
    func addShadow() {
        let shadowLayer = CALayer()
        shadowLayer.name = "ShadowLayer"
        shadowLayer.shadowColor = UIColor.black.cgColor
        shadowLayer.shadowOpacity = 1
        shadowLayer.shadowOffset = .zero
        shadowLayer.shadowRadius = 10
        shadowLayer.shadowPath = UIBezierPath(rect: shadowBackgroundView.bounds).cgPath
        // Otherwise the shadow will appear above the dummyView
        shadowLayer.zPosition = -1
        shadowBackgroundView.layer.addSublayer(shadowLayer)
    }

    @objc
    func removeShadow() {
        // Alternatively, you could also create the shadowLayer as a property, so you could call shadowLayer.removeFromSuperLayer()
        shadowBackgroundView.layer.sublayers?.first { $0.name == "ShadowLayer" }?.removeFromSuperlayer()
    }
}

注意,对于UITableViewCell,您不需要添加shadowBackgroundView,但您可以shadowLayer直接将 添加到cell.view.layer,作为cell.contentView.

于 2020-05-20T10:30:55.793 回答