[英]Add drop-shadow to an image when click on a button - iOS Swift
I'm trying to show the shadow of red/green behind my profile image when I clicked play/pause button. 单击播放/暂停按钮时,我试图在个人资料图像后面显示红色/绿色阴影。
I've tried 我试过了
@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
}
}
I couldn't seem to get it to work. 我似乎无法使其正常工作。 Can someone please give me a little hints on how to achive what I am after.
有人可以给我一些提示,告诉我如何实现我的追求。
If you set clipsToBounds
to true
, this will round the corners but prevent the shadow from appearing. 如果将
clipsToBounds
设置为true
,这将使拐角变圆,但防止阴影出现。 In order to resolve this, you can create two views. 为了解决此问题,您可以创建两个视图。 The container view should have the shadow, and its subview should have the rounded corners.
容器视图应具有阴影,其子视图应具有圆角。
The container view has clipsToBounds
set to false
, and has the shadow properties applied. 容器视图的
clipsToBounds
设置为false
,并且应用了阴影属性。 If you want the shadow to be rounded as well, use the UIBezierPath
constructor that takes in a roundedRect
and cornerRadius
. 如果您还希望阴影也被四舍五入,请使用具有
roundedRect
和cornerRadius
的UIBezierPath
构造函数。
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
Next, set the image view (or any other type of UIView
) to be the same size of the container view, set clipsToBounds
to true
, and give it a cornerRadius
. 接下来,将图像视图(或其他任何类型的
UIView
)设置为与容器视图相同的大小,将clipsToBounds
设置为true
,并将其设置为cornerRadius
。
let myImage = UIImageView(frame: outerView.bounds)
myImage.clipsToBounds = true
myImage.layer.cornerRadius = 10
Finally, remember to make the image view a subview of the container view. 最后,请记住使图像视图成为容器视图的子视图。
outerView.addSubview(myImage)
Ok, you can do something like this: 好的,您可以执行以下操作:
// 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
Then in your awake from nib method: 然后在您从笔尖唤醒的方法中:
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
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.