/
KJViewController.m
142 lines (115 loc) · 4.54 KB
/
KJViewController.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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
//
// KJViewController.m
// KJGridLayoutDemo
//
// Copyright (C) 2012 Kristopher Johnson
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
#import "KJViewController.h"
@implementation KJViewController
@synthesize gridLayout;
- (void)dealloc {
[gridLayout release];
[super dealloc];
}
- (UIButton *)buttonInViewWithTitle:(NSString *)title {
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[button setTitle:title forState:UIControlStateNormal];
[self.view addSubview:button];
return button;
}
- (void)makeKeypad {
UIButton *button;
// Create 1-9 keys in 3x3 grid
for (NSUInteger i = 0; i < 9; ++i) {
NSString *title = [NSString stringWithFormat:@"%u", (unsigned) i + 1];
button = [self buttonInViewWithTitle:title];
NSUInteger rowIndex = 3 - (i / 3);
NSUInteger columnIndex = i % 3;
[gridLayout addView:button row:rowIndex column:columnIndex];
}
// 0 key
button = [self buttonInViewWithTitle:@"0"];
[gridLayout addView:button row:4 column:0 columnSpan:2];
// Decimal
button = [self buttonInViewWithTitle:@"."];
[gridLayout addView:button row:4 column:2];
// Enter
button = [self buttonInViewWithTitle:@"Enter"];
[gridLayout addView:button row:3 rowSpan:2 column:3];
// +
button = [self buttonInViewWithTitle:@"+"];
[gridLayout addView:button row:2 column:3];
// -
button = [self buttonInViewWithTitle:@"-"];
[gridLayout addView:button row:1 column:3];
// *
button = [self buttonInViewWithTitle:@"*"];
[gridLayout addView:button row:0 column:3];
// /
button = [self buttonInViewWithTitle:@"/"];
[gridLayout addView:button row:0 column:2];
// Info text
UILabel *label = [[UILabel alloc] initWithFrame:CGRectZero];
label.text = @"This app demonstrates use of KJGridLayout";
label.numberOfLines = 0;
label.lineBreakMode = UILineBreakModeWordWrap;
label.textAlignment = UITextAlignmentCenter;
label.backgroundColor = [UIColor clearColor];
[self.view addSubview:label];
[gridLayout addView:label row:0 column:0 columnSpan:2];
[label release];
}
- (CGRect)layoutBounds {
CGRect layoutBounds = self.view.bounds;
layoutBounds = CGRectInset(layoutBounds, 8, 8);
return layoutBounds;
}
- (void)viewDidLoad {
[super viewDidLoad];
gridLayout = [[KJGridLayout alloc] init];
[gridLayout setColumnSpacing:4];
[gridLayout setRowSpacing:4];
[self makeKeypad];
[gridLayout setBounds:[self layoutBounds]];
[gridLayout layoutViews];
}
- (void)viewDidUnload {
// Release the grid layout to release the retained subviews
[self setGridLayout:nil];
[super viewDidUnload];
}
- (void)animateLayoutWithDuration:(NSTimeInterval)duration {
[UIView animateWithDuration:duration animations:^{
[gridLayout setBounds:[self layoutBounds]];
[gridLayout layoutViews];
}];
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone) {
return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
} else {
return YES;
}
}
- (void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration {
[super willAnimateRotationToInterfaceOrientation:toInterfaceOrientation duration:duration];
[self animateLayoutWithDuration:duration];
}
@end