Code snippets for the blog post "Touchy Feely" http://www.apokrupto.com/blog-1/2017/2/11/touchy-feely
Last active
February 12, 2017 15:12
-
-
Save warren-gavin/7a656efee9a923e8f54d9ef09ebafbd1 to your computer and use it in GitHub Desktop.
Code snippets for blog "Touchy-feely"
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
protocol ExtendedTouchable: class { | |
var touchableSurface: UIView? { get set } | |
func setupTouchableArea() | |
} | |
extension ExtendedTouchable where Self: UIControl { | |
func setupTouchableArea() { | |
touchableSurface = { | |
let view = UIView() | |
view.backgroundColor = .clear | |
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 | |
}() | |
} | |
} |
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
class Button: UIButton, ExtendedTouchable { | |
var touchableSurface: UIView? | |
override func layoutSubviews() { | |
super.layoutSubviews() | |
setupTouchableArea() | |
} | |
override func hitTest(_ point: CGPoint, with event: UIEvent?) -> UIView? { | |
guard let touchableSurface = touchableSurface, touchableSurface.frame.contains(point) else { | |
return super.hitTest(point, with: event) | |
} | |
return self | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment