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

vcirk6k6  于 2023-05-02  发布在  iOS
关注(0)|答案(2)|浏览(177)

我试图使我的标志形象的一些阴影效果,但效果并不炫耀。.

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
}
7kjnsjlb

7kjnsjlb1#

基本上,你不能让 * 裁剪角 * 和 * 阴影 * 重叠视图边界。
要实现这一点,您需要有两个视图-一个用于剪裁,一个用于阴影。

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
t9eec4r0

t9eec4r02#

你能试试这个吗?或者改为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

相关问题