Swift 3:
touchesBegan touchesEnded UIView
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
self.layer.backgroundColor = UIColor.white.cgColor
}
override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {
self.layer.backgroundColor = UIColor.gray.cgColor
}
CardView, , https://github.com/aclissold/CardView
@IBDesignable
class CardView: UIView {
@IBInspectable var cornerRadius: CGFloat = 5
@IBInspectable var shadowOffsetWidth: Int = 0
@IBInspectable var shadowOffsetHeight: Int = 1
@IBInspectable var shadowColor: UIColor? = UIColor.black
@IBInspectable var shadowOpacity: Float = 0.23
override func layoutSubviews() {
layer.cornerRadius = cornerRadius
let shadowPath = UIBezierPath(roundedRect: bounds, cornerRadius: cornerRadius)
layer.masksToBounds = false
layer.shadowColor = shadowColor?.cgColor
layer.shadowOffset = CGSize(width: shadowOffsetWidth, height: shadowOffsetHeight);
layer.shadowOpacity = shadowOpacity
layer.shadowPath = shadowPath.cgPath
}
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
layer.shadowOffset = CGSize(width: shadowOffsetWidth, height: shadowOffsetHeight * 2);
}
override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {
layer.shadowOffset = CGSize(width: shadowOffsetWidth, height:
shadowOffsetHeight);
}
}
:
touchesCancelled UIView .
override func touchesCancelled(_ touches: Set<UITouch>, with event: UIEvent?) {
layer.shadowOffset = CGSize(width: shadowOffsetWidth, height: shadowOffsetHeight);
}