forked from eway2012/SampleCode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathViewController.m
More file actions
254 lines (218 loc) · 8.36 KB
/
ViewController.m
File metadata and controls
254 lines (218 loc) · 8.36 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
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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
/*
File: ViewController.m
Abstract:
Version: 1.2
Disclaimer: IMPORTANT: This Apple software is supplied to you by Apple
Inc. ("Apple") in consideration of your agreement to the following
terms, and your use, installation, modification or redistribution of
this Apple software constitutes acceptance of these terms. If you do
not agree with these terms, please do not use, install, modify or
redistribute this Apple software.
In consideration of your agreement to abide by the following terms, and
subject to these terms, Apple grants you a personal, non-exclusive
license, under Apple's copyrights in this original Apple software (the
"Apple Software"), to use, reproduce, modify and redistribute the Apple
Software, with or without modifications, in source and/or binary forms;
provided that if you redistribute the Apple Software in its entirety and
without modifications, you must retain this notice and the following
text and disclaimers in all such redistributions of the Apple Software.
Neither the name, trademarks, service marks or logos of Apple Inc. may
be used to endorse or promote products derived from the Apple Software
without specific prior written permission from Apple. Except as
expressly stated in this notice, no other rights or licenses, express or
implied, are granted by Apple herein, including but not limited to any
patent rights that may be infringed by your derivative works or by other
works in which the Apple Software may be incorporated.
The Apple Software is provided by Apple on an "AS IS" basis. APPLE
MAKES NO WARRANTIES, EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION
THE IMPLIED WARRANTIES OF NON-INFRINGEMENT, MERCHANTABILITY AND FITNESS
FOR A PARTICULAR PURPOSE, REGARDING THE APPLE SOFTWARE OR ITS USE AND
OPERATION ALONE OR IN COMBINATION WITH YOUR PRODUCTS.
IN NO EVENT SHALL APPLE BE LIABLE FOR ANY SPECIAL, INDIRECT, INCIDENTAL
OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
INTERRUPTION) ARISING IN ANY WAY OUT OF THE USE, REPRODUCTION,
MODIFICATION AND/OR DISTRIBUTION OF THE APPLE SOFTWARE, HOWEVER CAUSED
AND WHETHER UNDER THEORY OF CONTRACT, TORT (INCLUDING NEGLIGENCE),
STRICT LIABILITY OR OTHERWISE, EVEN IF APPLE HAS BEEN ADVISED OF THE
POSSIBILITY OF SUCH DAMAGE.
Copyright (C) 2010 Apple Inc. All Rights Reserved.
*/
#import "ViewController.h"
#import "EAGLView.h"
#define DEG2RAD (M_PI/180.0f)
// These enums match the button tags in the nib
enum {
BUTTON_BRIGHTNESS,
BUTTON_CONTRAST,
BUTTON_SATURATION,
BUTTON_HUE,
BUTTON_SHARPNESS,
NUM_BUTTONS
};
@implementation ViewController
@synthesize slider;
@synthesize tabBar;
- (void)viewDidLoad
{
int b, i;
// Select first tab by default
tabBar.selectedItem = [tabBar.items objectAtIndex:0];
// Create a bitmap context for rendering the tabBar buttons
// Usually, button images are loaded from disk, but these simple shapes can be procedurally generated.
// UITabBar only needs the alpha channel of these images.
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
CGContextRef context = CGBitmapContextCreate(nil, 30, 30, 8, 0, colorSpace, kCGImageAlphaPremultipliedFirst);
CGImageRef theCGImage;
// Draw with white round strokes
CGContextSetLineCap(context, kCGLineCapRound);
CGContextSetRGBStrokeColor(context, 1.0, 1.0, 1.0, 1.0);
CGContextSetRGBFillColor(context, 1.0, 1.0, 1.0, 1.0);
CGContextSetLineWidth(context, 2.0);
for (b = 0; b < NUM_BUTTONS; b++)
{
CGContextClearRect(context, CGRectMake(0, 0, 30, 30));
switch(b)
{
case BUTTON_BRIGHTNESS:
{
const CGFloat line[8*4] = {
15.0, 6.0, 15.0, 4.0,
15.0,24.0, 15.0,26.0,
6.0,15.0, 4.0,15.0,
24.0,15.0, 26.0,15.0,
21.5,21.5, 23.0,23.0,
8.5, 8.5, 7.0, 7.0,
21.5, 8.5, 23.0, 7.0,
8.5,21.5, 7.0,23.0,
};
// A circle with eight rays around it
CGContextStrokeEllipseInRect(context, CGRectMake(10.5, 10.5, 9.0, 9.0));
for (i = 0; i < 8; i++)
{
CGContextMoveToPoint(context, line[i*4+0], line[i*4+1]);
CGContextAddLineToPoint(context, line[i*4+2], line[i*4+3]);
CGContextStrokePath(context);
}
break;
}
case BUTTON_CONTRAST:
{
// A circle with the right half filled
CGContextStrokeEllipseInRect(context, CGRectMake(4.0, 4.0, 22.0, 22.0));
CGContextAddArc(context, 15.0, 15.0, 11.0, -M_PI/2.0, M_PI/2.0, false);
CGContextFillPath(context);
break;
}
case BUTTON_SATURATION:
{
CGGradientRef gradient;
const CGFloat stripe[3][12] = {
{ 0.3,0.3,0.3,0.15, 1.0,0.0,0.0,0.70, 5, 5, 7, 25 },
{ 0.5,0.5,0.5,0.25, 0.0,1.0,0.0,0.75, 12, 5, 6, 25 },
{ 0.2,0.2,0.2,0.10, 0.0,0.0,1.0,0.65, 18, 5, 7, 25 },
};
// Red/Green/Blue gradients, inside a rounded rect
for (i = 0; i < 3; i++)
{
gradient = CGGradientCreateWithColorComponents(colorSpace, stripe[i], NULL, 2);
CGContextSaveGState(context);
CGContextClipToRect(context, CGRectMake(stripe[i][8], stripe[i][9], stripe[i][10], stripe[i][11]));
CGContextDrawLinearGradient(context, gradient, CGPointMake(15, 5), CGPointMake(15, 25), 0);
CGContextRestoreGState(context);
CGGradientRelease(gradient);
}
CGContextMoveToPoint(context, 4, 15);
CGContextAddArcToPoint(context, 4, 4, 15, 4, 4);
CGContextAddArcToPoint(context, 26, 4, 26, 15, 4);
CGContextAddArcToPoint(context, 26, 26, 15, 26, 4);
CGContextAddArcToPoint(context, 4, 26, 4, 15, 4);
CGContextClosePath(context);
CGContextStrokePath(context);
break;
}
case BUTTON_HUE:
{
CGGradientRef gradient;
CGFloat hue[8];
const int angle = 4;
// A radial gradient, inside a circle
for (i = 0; i < 360; i+=angle)
{
float x = cosf((i+angle*0.5)*DEG2RAD)*10+15;
float y = sinf((i+angle*0.5)*DEG2RAD)*10+15;
float r = (i )/180.0; if (r > 1.0) r = 2.0-r;
float g = (i+120)/180.0; if (g > 2.0) g = g-2.0; else if (g > 1.0) g = 2.0-g;
float b = (i+240)/180.0; if (b > 3.0) b = 4.0-b; else if (b > 2.0) b = b-2.0; else b = 2.0-b;
float a = (i+ 90)/180.0; if (a > 2.0) a = a-2.0; else if (a > 1.0) a = 2.0-a;
hue[0] = hue[4] = r;
hue[1] = hue[5] = g;
hue[2] = hue[6] = b;
hue[3] = a*0.5;
hue[7] = a*0.75;
gradient = CGGradientCreateWithColorComponents(colorSpace, hue, NULL, 2);
CGContextSaveGState(context);
CGContextMoveToPoint(context, 15, 15);
CGContextAddArc(context, 15, 15, 10, i*DEG2RAD, (i+angle)*DEG2RAD, false);
CGContextClosePath(context);
CGContextClip(context);
CGContextDrawLinearGradient(context, gradient, CGPointMake(x, y), CGPointMake(15, 15), 0);
CGContextRestoreGState(context);
CGGradientRelease(gradient);
}
CGContextStrokeEllipseInRect(context, CGRectMake(4.0, 4.0, 22.0, 22.0));
break;
}
case BUTTON_SHARPNESS:
{
int x, y;
// A gradient checkerboard, inside a rounded rect
for (x = 5; x < 25; x+=2)
{
float b = (x - 5)/19.0*0.5+0.375;
if (b > 0.75) b = 0.75;
else if (b < 0.5) b = 0.5;
for (y = 5; y < 25; y+=2)
{
float k = ((x ^ y) & 2) ? b : 1.0-b;
CGContextSetRGBFillColor(context, k, k, k, k);
CGContextFillRect(context, CGRectMake(x, y, 2, 2));
}
}
CGContextSetRGBFillColor(context, 1.0, 1.0, 1.0, 1.0);
CGContextMoveToPoint(context, 4, 15);
CGContextAddArcToPoint(context, 4, 4, 15, 4, 4);
CGContextAddArcToPoint(context, 26, 4, 26, 15, 4);
CGContextAddArcToPoint(context, 26, 26, 15, 26, 4);
CGContextAddArcToPoint(context, 4, 26, 4, 15, 4);
CGContextClosePath(context);
CGContextStrokePath(context);
break;
}
}
theCGImage = CGBitmapContextCreateImage(context);
((UITabBarItem *)[tabBar.items objectAtIndex:b]).image = [UIImage imageWithCGImage:theCGImage];
CGImageRelease(theCGImage);
}
CGContextRelease(context);
CGColorSpaceRelease(colorSpace);
}
- (void)dealloc
{
self.slider = nil;
self.tabBar = nil;
[super dealloc];
}
- (void)sliderAction:(id)sender
{
// Redraw the view with the new settings
[((EAGLView*)self.view) drawView];
}
- (void)tabBar:(UITabBar *)tabBar didSelectItem:(UITabBarItem *)item
{
// Recenter the slider (this application does not accumulate multiple filters)
[self.slider setValue:1.0 animated:YES];
// Redraw the view with the new settings
[((EAGLView*)self.view) drawView];
}
@end