|
// |
|
// Button with: |
|
// - designable cornerRadius |
|
// - designable borderWidth |
|
// - designable borderColor |
|
// - optional actions for touchDown, touchExit, touchUp |
|
// |
|
|
|
import UIKit |
|
|
|
typealias FRButtonAction = (FRButton) -> Void |
|
|
|
@IBDesignable |
|
|
|
public class FRButton: UIButton { |
|
|
|
var touchDownAction: FRButtonAction? |
|
var touchUpAction: FRButtonAction? |
|
var touchExitAction: FRButtonAction? |
|
|
|
public required init?(coder aDecoder: NSCoder) { |
|
super.init(coder: aDecoder) |
|
setupActions() |
|
} |
|
|
|
public override init(frame: CGRect) { |
|
super.init(frame: frame) |
|
setupActions() |
|
} |
|
|
|
@IBInspectable var borderWidth: CGFloat = 0.0 { |
|
didSet { |
|
layer.borderWidth = borderWidth |
|
} |
|
} |
|
|
|
@IBInspectable var borderColor: UIColor? { |
|
didSet { |
|
layer.borderColor = borderColor?.cgColor |
|
} |
|
} |
|
|
|
@IBInspectable override var cornerRadius: CGFloat { |
|
didSet { |
|
layer.cornerRadius = cornerRadius |
|
layer.masksToBounds = cornerRadius > 0 |
|
} |
|
} |
|
} |
|
|
|
extension FRButton { |
|
|
|
func setupActions() { |
|
addTarget(self, action: #selector(touchDown(sender:)), for: [.touchDown, .touchDragEnter]) |
|
addTarget(self, action: #selector(touchExit(sender:)), for: [.touchCancel, .touchDragExit]) |
|
addTarget(self, action: #selector(touchUp(sender:)), for: [.touchUpInside]) |
|
} |
|
|
|
//actions |
|
func touchDown(sender: FRButton) { |
|
touchDownAction?(sender) |
|
} |
|
|
|
func touchExit(sender: FRButton) { |
|
touchExitAction?(sender) |
|
} |
|
|
|
func touchUp(sender: FRButton) { |
|
touchUpAction?(sender) |
|
} |
|
} |