-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathTimer.m
More file actions
67 lines (58 loc) · 2.24 KB
/
Timer.m
File metadata and controls
67 lines (58 loc) · 2.24 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
#include <Cocoa/Cocoa.h>
@interface Window : NSWindow {
NSTextField* label;
NSButton* button;
NSTimer* timer;
int counter;
}
- (instancetype)init;
- (void)onButtonClick:(id)sender;
- (void)onTimerTick:(NSTimer*)timer;
- (BOOL)windowShouldClose:(id)sender;
@end
@implementation Window
- (instancetype)init {
counter = 0;
label = [[[NSTextField alloc] initWithFrame:NSMakeRect(10, 50, 210, 70)] autorelease];
[label setStringValue:[NSString stringWithFormat:@"%.1f", (float)counter / 10]];
//[label setStringValue:@"0.0"];
[label setBezeled:NO];
[label setDrawsBackground:NO];
[label setEditable:NO];
[label setSelectable:NO];
[label setTextColor:[NSColor colorWithRed:0.117 green:0.565 blue:1.0 alpha:1.0]];
[label setFont:[[NSFontManager sharedFontManager] convertFont:[[NSFontManager sharedFontManager] convertFont:[NSFont fontWithName:@"Arial" size:64] toHaveTrait:NSFontBoldTrait] toHaveTrait:NSFontItalicTrait]];
button = [[[NSButton alloc] initWithFrame:NSMakeRect(10, 10, 90, 32)] autorelease];
[button setAction:@selector(onButtonClick:)];
[button setBezelStyle:NSBezelStyleRounded];
[button setTitle:@"Start"];
[super initWithContentRect:NSMakeRect(100, 100, 230, 130) styleMask:NSWindowStyleMaskTitled | NSWindowStyleMaskClosable | NSWindowStyleMaskMiniaturizable | NSWindowStyleMaskResizable backing:NSBackingStoreBuffered defer:NO];
[self setTitle:@"Label Example"];
[[self contentView] addSubview:label];
[[self contentView] addSubview:button];
[self setIsVisible:YES];
return self;
}
- (void)onButtonClick:(id)sender {
if ([[button title] isEqual: @"Start"]) {
timer = [NSTimer timerWithTimeInterval:0.1f target:self selector:@selector(onTimerTick:) userInfo:self repeats:YES];
[[NSRunLoop mainRunLoop] addTimer:timer forMode:NSDefaultRunLoopMode];
[button setTitle:@"Stop"];
} else {
[timer invalidate];
[button setTitle:@"Start"];
}
}
- (void)onTimerTick:(NSTimer*)timer {
[label setStringValue:[NSString stringWithFormat:@"%.1f", (float)++counter / 10]];
}
- (BOOL)windowShouldClose:(id)sender {
[NSApp terminate:sender];
return YES;
}
@end
int main(int argc, char* argv[]) {
[NSApplication sharedApplication];
[[[[Window alloc] init] autorelease] makeMainWindow];
[NSApp run];
}