您的位置:首页 > 其它

Sheets(Chapter 25 of Cocoa Programming for Mac OS X)

2011-03-08 17:36 661 查看
1 //
2 // AppController.m
3 // TypingTutor
4 //
5 // Created by b1mobile on 3/3/11.
6 // Copyright 2011 __MyCompanyName__. All rights reserved.
7 //
8
9 #import "AppController.h"
10 #import "BigLetterView.h"
11
12 #define MAX_COUNT (100)
13 //#define COUNT_STEP (5)
14
15 @implementation AppController
16
17 - (id)init
18 {
19 [super init];
20
21 letters = [[NSArray alloc] initWithObjects:@"a", @"s", @"d", @"f", @"j", @"k", @"l", @";", nil];
22 srandom(time(NULL));
23 stepSize = 5;
24 return self;
25 }
26
27 - (void)awakeFromNib
28 {
29 [self showAnotherLetter];
30 }
31
32 - (void)resetCount
33 {
34 [self willChangeValueForKey:@"count"];
35 count = 0;
36 [self didChangeValueForKey:@"count"];
37 }
38
39 - (void)incrementCount
40 {
41 [self willChangeValueForKey:@"count"];
42 count = count + stepSize;
43 if (count > MAX_COUNT)
44 {
45 count = MAX_COUNT;
46 }
47 [self didChangeValueForKey:@"count"];
48 }
49
50 - (void)showAnotherLetter
51 {
52 int x = lastIndex;
53 while (x == lastIndex)
54 {
55 x = random() % [letters count];
56 }
57 lastIndex = x;
58 [outLetterView setString:[letters objectAtIndex:x]];
59
60 [self resetCount];
61 }
62
63 - (IBAction)stopGo:(id)sender
64 {
65 if (timer == nil)
66 {
67 NSLog(@"Starting");
68
69 timer = [[NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(checkThem) userInfo:nil repeats:YES] retain];
70 }
71 else
72 {
73 NSLog(@"Stopping");
74 [timer invalidate];
75 [timer release];
76 timer = nil;
77 }
78 }
79
80 - (void)checkThem:(NSTimer *)aTimer
81 {
82 if([[inLetterView string] isEqual:[outLetterView string]])
83 {
84 [self showAnotherLetter];
85 }
86 if (count == MAX_COUNT)
87 {
88 NSBeep();
89 [self resetCount];
90 }
91 else
92 {
93 [self incrementCount];
94 }
95 }
96
97 - (IBAction)showSpeedSheet:(id)sender
98 {
99 [NSApp beginSheet:speedSheet modalForWindow:[inLetterView window] modalDelegate:nil didEndSelector:NULL contextInfo:NULL];
}

- (IBAction)endSpeedSheet:(id)sender
{
[NSApp endSheet:speedSheet];
[speedSheet orderOut:sender];
}

@end
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: