【问题标题】:Add drop-shadow to an image when click on a button - iOS Swift单击按钮时向图像添加阴影 - iOS Swift
【发布时间】:2019-06-29 13:36:03
【问题描述】:

当我点击播放/暂停按钮时,我试图在我的个人资料图片后面显示红色/绿色的阴影。

我试过了

@objc func toggleButton(_ sender: UIButton) {

    //Play
    if sender.image(for: .normal) == UIImage(named: "play") {

        profileImage.layer.borderColor  = UIColor(hexString: "#8e8e8e").cgColor
        profileImage.layer.shadowColor = UIColor(hexString: "#099d57").cgColor
        profileImage.layer.cornerRadius = profileImage.frame.width / 2
        profileImage.layer.shadowOpacity = 1
        profileImage.layer.shadowOffset = CGSize.zero
        profileImage.layer.shadowRadius = 10
        profileImage.layer.shouldRasterize = true
        profileImage.layer.masksToBounds = false

    } else {
    //Pause

        profileImage.layer.borderColor  = UIColor(hexString: "#434343").cgColor
        profileImage.layer.shadowColor = UIColor(hexString: "#df544a").cgColor
        profileImage.layer.cornerRadius = profileImage.frame.width / 2
        profileImage.layer.shadowOpacity = 1
        profileImage.layer.shadowOffset = CGSize.zero
        profileImage.layer.shadowRadius = 10
        profileImage.layer.shouldRasterize = true
        profileImage.layer.masksToBounds = false
    }
}

我似乎无法让它工作。有人可以给我一些关于如何实现我所追求的提示。

【问题讨论】:

  • 问题是,您需要使阴影可见,因此您需要将masksToBoundsclipsToBounds 设置为false,但是这样您的视图将不会被四舍五入。现在怎么办?哦,所以我找到了这个答案,这正是您在圆角UIImageView 后面有阴影所需要的答案:creating a shadow for a uiimageview that has rounded corners

标签: ios swift uitableview uiimageview uiimage


【解决方案1】:

好的,你可以这样做:

// Replace this:
@IBOutlet weak var profileImage: UIImageView!
// With this:
@IBOutlet weak var profileImageContainer: UIView! // Change this to a UIView both in your storyboard or nib and here.
private weak var profileImageView: UIImageView? // Add this new variable

然后在你从 nib 方法中醒来:

override func awakeFromNib() {
        super.awakeFromNib()

        /* rest of your setup */
        this.setupProfileImageView()    
    }

private func setupProfileImageView() -> Void {
    if let superview = self.profileImageContainer {
        superview.clipsToBounds = false
        let imageView = UIImageView(frame: superview.bounds)
        superview.addSubview(imageView)
        imageView.layer.cornerRadius = imageView.frame.height / 2.0
        imageView.layer.borderColor = // your color
        imageView.clipsToBounds = true
        imageView.contentMode = .scaleAspectFill
        imageView.image = //your profile image here
        self.profileImageView = imageView
        // You may add constraints to pin your imageview here if you'd like. I would recommend that.
    }
}

@objc func toggleButton(_ sender: UIButton) {

    // Here I would try to compare the state if you are updating your state correctly instead of comparing the image. If this doesn't work, you may stick to what works for you
    if sender.state == .normal {

        self.profileImageView?.layer.borderColor  = UIColor(hexString: "#8e8e8e").cgColor
        self.profileImageContainer.layer.shadowColor = UIColor(hexString: "#099d57").cgColor
        self.profileImageContainer.layer.shadowOpacity = 1
        self.profileImageContainer.layer.shadowOffset = CGSize.zero
        self.profileImageContainer.layer.shadowRadius = 10
        self.profileImageContainer.layer.shouldRasterize = true
    } else {
    //Pause
        self.profileImageContainer.layer.borderColor  = UIColor(hexString: "#434343").cgColor
        self.profileImageContainer.layer.shadowColor = UIColor(hexString: "#df544a").cgColor
        self.profileImageContainer.layer.shadowOpacity = 1
        self.profileImageContainer.layer.shadowOffset = CGSize.zero
        self.profileImageContainer.layer.shadowRadius = 10
        self.profileImageContainer.layer.shouldRasterize = true
    }
}

【讨论】:

  • 感谢 Pranay,但我在私人功能上得到了这个。 :i.imgur.com/MYsyG17.png
  • @kyo 那是因为你需要用任何适用的值替换我的 cmets。像这一行:imageView.layer.borderColor = // your color 而不是 // your color 为该图像视图的边框颜色输入您想要的任何颜色。也许是:imageView.layer.borderColor = UIColor(hexString: "#8e8e8e").cgColor。完成此操作后,删除 as! CGColor as! Bool。然后在这里:imageView.image = //your profile image here 如果你有图像,请设置图像,否则如果你在其他地方设置它,则完全删除此行。
  • @kyo 你看我的评论了吗?如果需要,可以在此处设置图像或删除此行:imageView.image = //your profile image here
【解决方案2】:

如果您将clipsToBounds 设置为true,这将使角落变圆,但不会出现阴影。为了解决这个问题,您可以创建两个视图。容器视图应该有阴影,它的子视图应该有圆角。

容器视图将clipsToBounds 设置为false,并应用了阴影属性。如果您还希望阴影圆润,请使用UIBezierPath 构造函数,该构造函数接受roundedRectcornerRadius

let outerView = UIView(frame: profileImage.layer.frame)
outerView.clipsToBounds = false
outerView.layer.shadowColor = UIColor.black.cgColor
outerView.layer.shadowOpacity = 1
outerView.layer.shadowOffset = CGSize.zero
outerView.layer.shadowRadius = 10
outerView.layer.shadowPath = UIBezierPath(roundedRect: outerView.bounds, cornerRadius: 10).cgPath

接下来,将图像视图(或任何其他类型的UIView)设置为与容器视图相同的大小,将clipsToBounds 设置为true,并给它一个cornerRadius

let myImage = UIImageView(frame: outerView.bounds)
myImage.clipsToBounds = true
myImage.layer.cornerRadius = 10

最后,记得让图像视图成为容器视图的子视图。

outerView.addSubview(myImage)

【讨论】:

  • 我试过你的代码,当我点击我的播放/暂停按钮时它没有效果。看看这里的代码和结果:i.imgur.com/g5cuC1O.png
  • 当然你应该将outerView的框架设置为你的profileView。我更新了答案,请再次检查。
  • 您的最新代码仍然无法正常工作。很抱歉..我看到0效果。我可以使用您的确切代码并进行测试吗?
  • @kyo 那是因为你在设置之后就丢弃了你的outerView 实例。确切地说,就在您的 imgur 链接中的 outerView.addSubview(myImage) 行之后。您需要首先在容器中设置图像视图,例如在awakeFromNib() 或故事板/xib 文件本身中,然后在运行时在按钮操作函数中添加/删除阴影。
  • @kyo 正如我所说,您的问题是在此行之后的outerView.addSubview(myImage),您没有对outerView 实例执行任何操作。如果您根据您的设置执行self.view.addSubview(outerView)self.addSubview(outerView) 之类的操作,您应该会看到结果。但是,这不是实现这一点的好方法,因为每次用户单击按钮时它都会添加一个新的子视图。因此,您需要在开始时设置一次。这可以在界面生成器中,也可以在awakeFromNib() 或其他一些方法中
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-06-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多