Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import UIKit
- class ViewController: UIViewController {
- @IBOutlet weak var button : UIButton!
- override func viewDidLoad() {
- super.viewDidLoad()
- // Do any additional setup after loading the view, typically from a nib.
- button.setShadowStyle()
- }
- override func didReceiveMemoryWarning() {
- super.didReceiveMemoryWarning()
- // Dispose of any resources that can be recreated.
- }
- }
- extension UIButton {
- func setShadowStyle() {
- self.addTarget(self, action: #selector(pressUnshadowEffect), for: UIControlEvents.touchDown)
- self.addTarget(self, action: #selector(pressShadowEffect), for: UIControlEvents.touchUpInside)
- self.shadeButton()
- }
- func shadeButton() {
- self.layer.shadowOffset = CGSize(width: 0, height: 0)
- self.layer.shadowColor = UIColor.black.cgColor
- self.layer.shadowOpacity = 1.0
- self.layer.shadowRadius = 3.0
- self.layer.masksToBounds = false
- }
- func unshadeButton() {
- self.layer.shadowOffset = CGSize(width: 0, height: 0)
- self.layer.shadowColor = UIColor.clear.cgColor
- self.layer.shadowOpacity = 0.0
- self.layer.shadowRadius = 0.0
- self.layer.masksToBounds = false
- }
- @objc func pressShadowEffect() {
- UIView.performWithoutAnimation { [unowned self] in
- self.shadeButton()
- }
- }
- @objc func pressUnshadowEffect() {
- UIView.performWithoutAnimation { [unowned self] in
- self.unshadeButton()
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement