阴影效果不起作用...Swift ios uikit

问题描述 投票:0回答:2

我正在尝试在我的徽标图像上制作一些阴影效果,但效果并没有炫耀..

func setUpLogo(){
    view.addSubview(logoView)
    logoView.translatesAutoresizingMaskIntoConstraints = false
    logoView.topAnchor.constraint(equalTo: view.topAnchor,constant: 220).isActive = true
    logoView.widthAnchor.constraint(equalToConstant: 140).isActive = true
    logoView.heightAnchor.constraint(equalToConstant: 190).isActive = true
    logoView.centerXAnchor.constraint(equalTo: view.centerXAnchor).isActive  = true
    logoView.image = UIImage(named: "logoCharity")
    logoView.layer.cornerRadius = 11
    logoView.clipsToBounds = true
    logoView.layer.shadowColor = UIColor.red.cgColor
    logoView.layer.shadowOpacity = 1
    logoView.layer.shadowOffset = CGSize(width: 0, height: 0)
    logoView.layer.shadowRadius = 10
}
ios swift shadow
2个回答
0
投票

你能试试这个还是 改成 logoView.clipsToBounds = false

let imageView = UIImageView(image: UIImage(named: "your_image"))
imageView.layer.cornerRadius = 8.0
imageView.layer.masksToBounds = true
imageView.layer.shadowColor = UIColor.black.cgColor
imageView.layer.shadowOffset = CGSize(width: 0, height: 2)
imageView.layer.shadowOpacity = 0.5
imageView.layer.shadowRadius = 4

0
投票

基本上你不能同时有clipping cornersshadow重叠视图边界。

为此,您需要有两个视图 - 一个用于裁剪,一个用于阴影。

let container = UIView()
view.addSubview(container)
container.addSubview(logoView)

container.backgroundColor = .clear
container.clipsToBounds = false
container.layer.cornerRadius = 11
container.layer.shadowColor = UIColor.red.cgColor
container.layer.shadowOpacity = 1
container.layer.shadowOffset = .zero
container.layer.shadowRadius = 10

logoView.image = UIImage(named: "logoCharity")
logoView.clipsToBounds = true
logoView.layer.cornerRadius = 11

container.translatesAutoresizingMaskIntoConstraints = false
container.topAnchor.constraint(equalTo: view.topAnchor,constant: 220).isActive = true
container.widthAnchor.constraint(equalToConstant: 140).isActive = true
container.heightAnchor.constraint(equalToConstant: 190).isActive = true
container.centerXAnchor.constraint(equalTo: view.centerXAnchor).isActive  = true

logoView.translatesAutoresizingMaskIntoConstraints = false
logoView.leftAnchor.constraint(equalTo: container.leftAnchor).isActive = true
logoView.rightAnchor.constraint(equalTo: container.rightAnchor).isActive = true
logoView.topAnchor.constraint(equalTo: container.topAnchor).isActive = true
logoView.bottomAnchor.constraint(equalTo: container.bottomAnchor).isActive = true
© www.soinside.com 2019 - 2024. All rights reserved.