NSTimer(Chapter 24 of Cocoa Programming for Mac OS X)
1 #import "AppController.h"
2 #import "BigLetterView.h"
3
4 #define MAX_COUNT (100)
5 #define COUNT_STEP (5)
6
7 @implementation AppController
8
9 - (id)init
10 {
11 [super init];
12
13 letters = [[NSArray alloc] initWithObjects:@"a", @"s", @"d", @"f", @"j", @"k", @"l", @";", nil];
14 srandom(time(NULL));
15 return self;
16 }
17
18 - (void)awakeFromNib
19 {
20 [self showAnotherLetter];
21 }
22
23 - (void)resetCount
24 {
25 [self willChangeValueForKey:@"count"];
26 count = 0;
27 [self didChangeValueForKey:@"count"];
28 }
29
30 - (void)incrementCount
31 {
32 [self willChangeValueForKey:@"count"];
33 count = count + COUNT_STEP;
34 if (count > MAX_COUNT)
35 {
36 count = MAX_COUNT;
37 }
38 [self didChangeValueForKey:@"count"];
39 }
40
41 - (void)showAnotherLetter
42 {
43 int x = lastIndex;
44 while (x == lastIndex)
45 {
46 x = random() % [letters count];
47 }
48 lastIndex = x;
49 [outLetterView setString:[letters objectAtIndex:x]];
50
51 [self resetCount];
52 }
53
54 - (IBAction)stopGo:(id)sender
55 {
56 if (timer == nil)
57 {
58 NSLog(@"Starting");
59
60 timer = [[NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(checkThem) userInfo:nil repeats:YES] retain];
61 }
62 else
63 {
64 NSLog(@"Stopping");
65 [timer invalidate];
66 [timer release];
67 timer = nil;
68 }
69 }
70
71 - (void)checkThem:(NSTimer *)aTimer
72 {
73 if([[inLetterView string] isEqual:[outLetterView string]])
74 {
75 [self showAnotherLetter];
76 }
77 if (count == MAX_COUNT)
78 {
79 NSBeep();
80 [self resetCount];
81 }
82 else
83 {
84 [self incrementCount];
85 }
86
87 }
88
89 @end
2 #import "BigLetterView.h"
3
4 #define MAX_COUNT (100)
5 #define COUNT_STEP (5)
6
7 @implementation AppController
8
9 - (id)init
10 {
11 [super init];
12
13 letters = [[NSArray alloc] initWithObjects:@"a", @"s", @"d", @"f", @"j", @"k", @"l", @";", nil];
14 srandom(time(NULL));
15 return self;
16 }
17
18 - (void)awakeFromNib
19 {
20 [self showAnotherLetter];
21 }
22
23 - (void)resetCount
24 {
25 [self willChangeValueForKey:@"count"];
26 count = 0;
27 [self didChangeValueForKey:@"count"];
28 }
29
30 - (void)incrementCount
31 {
32 [self willChangeValueForKey:@"count"];
33 count = count + COUNT_STEP;
34 if (count > MAX_COUNT)
35 {
36 count = MAX_COUNT;
37 }
38 [self didChangeValueForKey:@"count"];
39 }
40
41 - (void)showAnotherLetter
42 {
43 int x = lastIndex;
44 while (x == lastIndex)
45 {
46 x = random() % [letters count];
47 }
48 lastIndex = x;
49 [outLetterView setString:[letters objectAtIndex:x]];
50
51 [self resetCount];
52 }
53
54 - (IBAction)stopGo:(id)sender
55 {
56 if (timer == nil)
57 {
58 NSLog(@"Starting");
59
60 timer = [[NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(checkThem) userInfo:nil repeats:YES] retain];
61 }
62 else
63 {
64 NSLog(@"Stopping");
65 [timer invalidate];
66 [timer release];
67 timer = nil;
68 }
69 }
70
71 - (void)checkThem:(NSTimer *)aTimer
72 {
73 if([[inLetterView string] isEqual:[outLetterView string]])
74 {
75 [self showAnotherLetter];
76 }
77 if (count == MAX_COUNT)
78 {
79 NSBeep();
80 [self resetCount];
81 }
82 else
83 {
84 [self incrementCount];
85 }
86
87 }
88
89 @end