Skip to content

Instantly share code, notes, and snippets.

@warren-gavin
Created February 12, 2017 14:30
Show Gist options
  • Save warren-gavin/25ddc11a27e3c75a8a8afb0881951786 to your computer and use it in GitHub Desktop.
Save warren-gavin/25ddc11a27e3c75a8a8afb0881951786 to your computer and use it in GitHub Desktop.
class Button: UIButton, ExtendedTouchable {
var touchableSurface: UIView?
override func layoutSubviews() {
super.layoutSubviews()
setupTouchableArea()
}
override func hitTest(_ point: CGPoint, with event: UIEvent?) -> UIView? {
guard isEnabled, !isHidden, alpha != 0.0 else {
return nil
}
guard let touchableSurface = touchableSurface, touchableSurface.frame.contains(point) else {
return super.hitTest(point, with: event)
}
return self
}
}
/// Touchable functionality, extended to cover a potentially larger surface area on screen
protocol ExtendedTouchable: class {
/// Subview that is the outline for the extended touchable area
var touchableSurface: UIView? { get set }
/// Create the extended touchable area
func setupTouchableArea()
}
// MARK: - UIControl
extension ExtendedTouchable where Self: UIControl {
func setupTouchableArea() {
if bounds.size.width > .minimumButtonDimension, bounds.size.height > .minimumButtonDimension {
touchableSurface?.removeFromSuperview()
touchableSurface = nil
return
}
if touchableSurface == nil {
touchableSurface = {
let view = UIView()
view.backgroundColor = .clear
// For debug
// backgroundColor = #colorLiteral(red: 0.06274509804, green: 0.1254901961, blue: 0.4117647059, alpha: 0.2008240582)
// view.backgroundColor = #colorLiteral(red: 0.06274509804, green: 0.1254901961, blue: 0.4117647059, alpha: 0.2008240582)
addSubview(view)
sendSubview(toBack: view)
view.translatesAutoresizingMaskIntoConstraints = false
view.centerXAnchor.constraint(equalTo: centerXAnchor).isActive = true
view.centerYAnchor.constraint(equalTo: centerYAnchor).isActive = true
view.widthAnchor.constraint (equalToConstant: max(bounds.size.width, .minimumButtonDimension)).isActive = true
view.heightAnchor.constraint (equalToConstant: max(bounds.size.height, .minimumButtonDimension)).isActive = true
return view
}()
}
}
}
// MARK: - CGFloat constants
private extension CGFloat {
static let minimumButtonDimension: CGFloat = 44.0
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment