Click to Roll

Your users have been clamoring for a way to roll the die. The designer thinks that double-clicking on it would be a sensible way to accomplish this, so let’s add it. Add a randomize() method to DieView:

 ​ ​ ​ ​f​u​n​c​ ​r​a​n​d​o​m​i​z​e​(​)​ ​{​
 ​ ​ ​ ​ ​ ​ ​ ​i​n​t​V​a​l​u​e​ ​=​ ​I​n​t​(​a​r​c​4​r​a​n​d​o​m​_​u​n​i​f​o​r​m​(​5​)​)​ ​+​ ​1​
 ​ ​ ​ ​}

In mouseUp(_:), use NSEvent’s clickCount to check for the double-click before calling randomize():

 ​ ​ ​ ​o​v​e​r​r​i​d​e​ ​f​u​n​c​ ​m​o​u​s​e​U​p​(​t​h​e​E​v​e​n​t​:​ ​N​S​E​v​e​n​t​)​ ​{​
 ​ ​ ​ ​ ​ ​ ​ ​p​r​i​n​t​l​n​(​"​m​o​u​s​e​U​p​ ​c​l​i​c​k​C​o​u​n​t​:​ ​\​(​t​h​e​E​v​e​n​t​.​c​l​i​c​k​C​o​u​n​t​)​"​)​
 ​ ​ ​ ​ ​ ​ ​ ​i​f​ ​t​h​e​E​v​e​n​t​.​c​l​i​c​k​C​o​u​n​t​ ...

Get Cocoa Programming for OS X: The Big Nerd Ranch Guide now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.