Last active
September 8, 2021 10:24
-
-
Save arjun011/e8644b72d01691b56f80d31fca0dc39b to your computer and use it in GitHub Desktop.
Apply round and shadow effect on UIView
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Foundation | |
import UIKit | |
@IBDesignable class RoundShadowView: UIView { | |
@IBInspectable var shadow: Bool { | |
get { | |
return layer.shadowOpacity > 0.0 | |
} | |
set { | |
if newValue == true { | |
self.addShadow() | |
} | |
} | |
} | |
@IBInspectable var cornerRadius: CGFloat { | |
get { | |
return self.layer.cornerRadius | |
} | |
set { | |
self.layer.cornerRadius = newValue | |
// Don't touch the masksToBound property if a shadow is needed in addition to the cornerRadius | |
if shadow == false { | |
self.layer.masksToBounds = true | |
} | |
} | |
} | |
func addShadow(shadowColor: CGColor = UIColor.black.cgColor, | |
shadowOffset: CGSize = CGSize(width: 1.0, height: 2.0), | |
shadowOpacity: Float = 0.7, | |
shadowRadius: CGFloat = 3.0) { | |
layer.shadowColor = shadowColor | |
layer.shadowOffset = shadowOffset | |
layer.shadowOpacity = shadowOpacity | |
layer.shadowRadius = shadowRadius | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment