April 2015
Intermediate to advanced
556 pages
17h 47m
English
The hit testing in DieView is pretty lazy. If the user clicks anywhere in the view, the die reacts. It would be better if the die only depressed when the user clicked in the die’s rectangle.
CGRect’s contains(_:) function can check whether a rectangle contains a point, and you can get the die’s rectangle from metricsForSize(_:).
There is a hurdle, however: NSEvent provides locationInWindow, but what you need is the point in the view’s coordinate system, which dieFrame is in. How can you convert locationInWindow to the correct coordinate system?
NSView provides a few methods for this very purpose:
func convertPoint(aPoint: NSPoint, fromView aView: NSView?) -> NSPoint func convertPoint(aPoint: NSPoint, ...