-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathController.m
63 lines (49 loc) · 2.26 KB
/
Controller.m
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
#import "Controller.h"
@implementation Controller
@synthesize polygon;
- (IBAction)decreaseSides:(id)sender {
[polygon setNumberOfSides:([polygon numberOfSides] - 1)];
[self updateInterface];
}
- (IBAction)increaseSides:(id)sender {
[polygon setNumberOfSides:([polygon numberOfSides] + 1)];
[self updateInterface];
}
- (void)awakeFromNib {
NSInteger sides = [[NSUserDefaults standardUserDefaults] integerForKey:@"numberOfSides"];
[numberOfSidesLabel setText:[NSString stringWithFormat:@"%i", sides]];
// instantiate the polygon object and keep it around
polygon = [[PolygonShape alloc] initWithNumberOfSides:sides];
// instantiate the UIView for polygon shape
PolygonShapeView *polygonShapeView = [[PolygonShapeView alloc] initWithFrame:CGRectMake(10.0, 160.0, 220.0, 140.0)];
[polygonShapeView setPolygon:polygon];
self.view = polygonShapeView;
[polygonShapeView release];
[self updateInterface];
}
- (void)viewWillDisappear:(BOOL)animated {
// save the current default to disk
NSInteger mySides = [polygon numberOfSides];
[[NSUserDefaults standardUserDefaults] setInteger:mySides forKey:@"numberOfSides"];
}
- (void)updateInterface {
// update number of sides, and enable/disable buttons w/alpha transparency calculations
int sides = [polygon numberOfSides];
increaseSidesButton.enabled = [polygon isValidMaximumNumberOfSides:(sides + 1)];
[increaseSidesButton setAlpha:(increaseSidesButton.enabled ? 1.0 : 0.3)];
decreaseSidesButton.enabled = [polygon isValidMinimumNumberOfSides:(sides - 1)];
[decreaseSidesButton setAlpha:(decreaseSidesButton.enabled ? 1.0 : 0.3)];
numberOfSidesLabel.text = [NSString stringWithFormat:@"%i", sides];
// update labels with information from the polygon
degreesLabel.text = [NSString stringWithFormat:@"%i", (int) [polygon angleInDegrees]];
radiansLabel.text = [NSString stringWithFormat:@"%.6f", [polygon angleInRadians]];
minSidesLabel.text = [NSString stringWithFormat:@"%i", [polygon minimumNumberOfSides]];
maxSidesLabel.text = [NSString stringWithFormat:@"%i", [polygon maximumNumberOfSides]];
// redraw the polygon
[self.view setNeedsDisplay];
}
- (void)dealloc {
[polygon release];
[super dealloc];
}
@end