Skip to content

Commit 28e2742

Browse files
committed
first commit
0 parents  commit 28e2742

File tree

12 files changed

+1255
-0
lines changed

12 files changed

+1255
-0
lines changed

Classes/AppDelegate.h

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
//
2+
// AppDelegate.h
3+
// Created by http://github.com/iosdeveloper
4+
//
5+
6+
#import <UIKit/UIKit.h>
7+
8+
@class ViewController;
9+
10+
@interface AppDelegate : NSObject <UIApplicationDelegate> {
11+
UIWindow *window;
12+
ViewController *viewController;
13+
}
14+
15+
@property (nonatomic, retain) IBOutlet UIWindow *window;
16+
@property (nonatomic, retain) IBOutlet ViewController *viewController;
17+
18+
@end

Classes/AppDelegate.m

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
//
2+
// AppDelegate.m
3+
// Created by http://github.com/iosdeveloper
4+
//
5+
6+
#import "AppDelegate.h"
7+
#import "ViewController.h"
8+
9+
@implementation AppDelegate
10+
11+
@synthesize window;
12+
@synthesize viewController;
13+
14+
#pragma mark -
15+
#pragma mark Application lifecycle
16+
17+
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
18+
// Override point for customization after application launch.
19+
20+
// Set the view controller as the window's root view controller and display.
21+
self.window.rootViewController = self.viewController;
22+
[self.window makeKeyAndVisible];
23+
24+
return YES;
25+
}
26+
27+
#pragma mark -
28+
#pragma mark Memory management
29+
30+
- (void)dealloc {
31+
[viewController release];
32+
[window release];
33+
[super dealloc];
34+
}
35+
36+
@end

Classes/ExpandyButton/ExpandyButton.h

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
#import <UIKit/UIKit.h>
2+
3+
4+
@interface ExpandyButton : UIButton {
5+
@private
6+
BOOL _expanded;
7+
UILabel *_titleLabel;
8+
CGRect _frameExpanded;
9+
CGRect _frameShrunk;
10+
CGFloat _buttonWidth;
11+
NSInteger _selectedItem;
12+
NSArray *_labels;
13+
}
14+
15+
- (id)initWithPoint:(CGPoint)point title:(NSString *)title buttonNames:(NSArray *)buttonNames selectedItem:(NSInteger)selectedItem;
16+
- (id)initWithPoint:(CGPoint)point title:(NSString *)title buttonNames:(NSArray *)buttonNames;
17+
18+
@property (nonatomic,readonly,retain) UILabel *titleLabel;
19+
@property (nonatomic,readonly,retain) NSArray *labels;
20+
@property (nonatomic,assign) NSInteger selectedItem;
21+
22+
@end

Classes/ExpandyButton/ExpandyButton.m

Lines changed: 225 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,225 @@
1+
2+
/*
3+
</codex>
4+
*/
5+
6+
#import "ExpandyButton.h"
7+
#import <QuartzCore/QuartzCore.h>
8+
9+
// Measurements
10+
static Boolean gInitMeasurements = false;
11+
static CGFloat gWidth = 47.f;
12+
static CGFloat gFrameHeight = 32.f;
13+
static CGFloat gTitleXOrigin = 8.f;
14+
static CGFloat gTitleYOrigin = 9.f;
15+
static CGFloat gTitleHeight = 15.f;
16+
static CGFloat gTitleWidth = 35.f;
17+
static CGFloat gButtonHeight = 26.f;
18+
static CGFloat gLabelHeight = 39.f;
19+
static CGFloat gLabelXOrigin = 45.f;
20+
static CGFloat gLabelYOrigin = -3.f;
21+
static CGFloat gDefaultButtonWidth = 44.f;
22+
static CGFloat gFontSize = 12.f;
23+
24+
// HUD Appearance
25+
static CGFloat gLayerWhite = 1.f;
26+
static CGFloat gLayerAlpha = .2f;
27+
static CGFloat gBorderWhite = .0f;
28+
static CGFloat gBorderAlpha = 1.f;
29+
static CGFloat gBorderWidth = 1.f;
30+
31+
32+
@interface ExpandyButton ()
33+
34+
@property (nonatomic,assign) BOOL expanded;
35+
@property (nonatomic,assign) CGRect frameExpanded;
36+
@property (nonatomic,assign) CGRect frameShrunk;
37+
@property (nonatomic,assign) CGFloat buttonWidth;
38+
@property (nonatomic,retain) UILabel *titleLabel;
39+
@property (nonatomic,retain) NSArray *labels;
40+
41+
@end
42+
43+
@implementation ExpandyButton
44+
45+
@synthesize expanded = _expanded;
46+
@synthesize frameExpanded = _frameExpanded;
47+
@synthesize frameShrunk = _frameShrunk;
48+
@synthesize buttonWidth = _buttonWidth;
49+
@synthesize titleLabel = _titleLabel;
50+
@synthesize labels = _labels;
51+
@dynamic selectedItem;
52+
53+
- (id)initWithPoint:(CGPoint)point title:(NSString *)title buttonNames:(NSArray *)buttonNames selectedItem:(NSInteger)selectedItem
54+
{
55+
if (!gInitMeasurements) {
56+
gInitMeasurements = true;
57+
const Boolean isIPad = ([[UIDevice currentDevice] userInterfaceIdiom] != UIUserInterfaceIdiomPhone);
58+
if (isIPad) {
59+
gWidth = 93.f;
60+
gFrameHeight = 64.f;
61+
gTitleXOrigin = 16.f;
62+
gTitleYOrigin = 18.f;
63+
gTitleHeight = 30.f;
64+
gTitleWidth = 75.f;
65+
gButtonHeight = 52.f;
66+
gLabelHeight = 78.f;
67+
gLabelXOrigin = 86.f;
68+
gLabelYOrigin = -6.f;
69+
gDefaultButtonWidth = 100.f;
70+
gFontSize = 24.f;
71+
72+
gBorderWidth = 2.f;
73+
}
74+
}
75+
76+
77+
CGRect frameShrunk = CGRectMake(point.x, point.y, gWidth + gDefaultButtonWidth, gFrameHeight);
78+
CGRect frameExpanded = CGRectMake(point.x, point.y, gWidth + gDefaultButtonWidth * [buttonNames count], gFrameHeight);
79+
if ((self = [super initWithFrame:frameShrunk])) {
80+
[self setFrameShrunk:frameShrunk];
81+
[self setFrameExpanded:frameExpanded];
82+
[self setButtonWidth:gDefaultButtonWidth];
83+
84+
UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(gTitleXOrigin, gTitleYOrigin, gTitleWidth, gTitleHeight)];
85+
[titleLabel setText:title];
86+
[titleLabel setFont:[UIFont systemFontOfSize:gFontSize]];
87+
[titleLabel setTextColor:[UIColor blackColor]];
88+
[titleLabel setBackgroundColor:[UIColor clearColor]];
89+
[self addSubview:titleLabel];
90+
[self setTitleLabel:titleLabel];
91+
[titleLabel release];
92+
93+
NSMutableArray *labels = [[NSMutableArray alloc] initWithCapacity:3];
94+
NSInteger index = 0;
95+
UILabel *label;
96+
for (NSString *buttonName in buttonNames) {
97+
label = [[UILabel alloc] initWithFrame:CGRectMake(gLabelXOrigin + (gDefaultButtonWidth * index), gLabelYOrigin, gDefaultButtonWidth, gButtonHeight)];
98+
[label setText:buttonName];
99+
[label setFont:[UIFont systemFontOfSize:gFontSize]];
100+
[label setTextColor:[UIColor blackColor]];
101+
[label setBackgroundColor:[UIColor clearColor]];
102+
[label setTextAlignment:UITextAlignmentCenter];
103+
[self addSubview:label];
104+
[labels addObject:label];
105+
[label release];
106+
index += 1;
107+
}
108+
109+
[self setLabels:labels];
110+
[labels release];
111+
112+
[self addTarget:self action:@selector(chooseLabel:forEvent:) forControlEvents:UIControlEventTouchUpInside];
113+
[self setBackgroundColor:[UIColor clearColor]];
114+
115+
CALayer *layer = [self layer];
116+
[layer setBackgroundColor:[[UIColor colorWithWhite:gLayerWhite alpha:gLayerAlpha] CGColor]];
117+
[layer setBorderWidth:gBorderWidth];
118+
[layer setBorderColor:[[UIColor colorWithWhite:gBorderWhite alpha:gBorderAlpha] CGColor]];
119+
[layer setCornerRadius:15.f];
120+
121+
[self setExpanded:YES];
122+
[self setHidden:YES];
123+
124+
[self setSelectedItem:selectedItem];
125+
}
126+
return self;
127+
}
128+
129+
- (id)initWithPoint:(CGPoint)point title:(NSString *)title buttonNames:(NSArray *)buttonNames
130+
{
131+
return [self initWithPoint:point title:title buttonNames:buttonNames selectedItem:0];
132+
}
133+
134+
- (void)chooseLabel:(id)sender forEvent:(UIEvent *)event
135+
{
136+
[UIView beginAnimations:nil context:NULL];
137+
if ([self expanded] == NO) {
138+
[self setExpanded:YES];
139+
140+
NSInteger index = 0;
141+
for (UILabel *label in [self labels]) {
142+
if (index == [self selectedItem]) {
143+
[label setFont:[UIFont boldSystemFontOfSize:gFontSize]];
144+
} else {
145+
[label setTextColor:[UIColor colorWithWhite:0.f alpha:.8f]];
146+
}
147+
[label setFrame:CGRectMake(gLabelXOrigin + ([self buttonWidth] * index), gLabelYOrigin, [self buttonWidth], gLabelHeight)];
148+
index += 1;
149+
}
150+
151+
[[self layer] setFrame:[self frameExpanded]];
152+
} else {
153+
BOOL inside = NO;
154+
155+
NSInteger index = 0;
156+
for (UILabel *label in [self labels]) {
157+
if ([label pointInside:[[[event allTouches] anyObject] locationInView:label] withEvent:event]) {
158+
[label setFrame:CGRectMake(gLabelXOrigin, gLabelYOrigin, [self buttonWidth], gLabelHeight)];
159+
inside = YES;
160+
break;
161+
}
162+
index += 1;
163+
}
164+
165+
if (inside) {
166+
[self setSelectedItem:index];
167+
}
168+
}
169+
[UIView commitAnimations];
170+
}
171+
172+
- (NSInteger)selectedItem
173+
{
174+
return _selectedItem;
175+
}
176+
177+
- (void)setSelectedItem:(NSInteger)selectedItem
178+
{
179+
if (selectedItem < [[self labels] count]) {
180+
CGRect leftShrink = CGRectMake(gLabelXOrigin, gLabelYOrigin, 0.f, gLabelHeight);
181+
CGRect rightShrink = CGRectMake(gLabelXOrigin + [self buttonWidth], gLabelYOrigin, 0.f, gLabelHeight);
182+
CGRect middleExpanded = CGRectMake(gLabelXOrigin, gLabelYOrigin, [self buttonWidth], gLabelHeight);
183+
NSInteger count = 0;
184+
BOOL expanded = [self expanded];
185+
186+
if (expanded) {
187+
[UIView beginAnimations:nil context:NULL];
188+
}
189+
190+
for (UILabel *label in [self labels]) {
191+
if (count < selectedItem) {
192+
[label setFrame:leftShrink];
193+
[label setFont:[UIFont systemFontOfSize:gFontSize]];
194+
} else if (count > selectedItem) {
195+
[label setFrame:rightShrink];
196+
[label setFont:[UIFont systemFontOfSize:gFontSize]];
197+
} else if (count == selectedItem) {
198+
[label setFrame:middleExpanded];
199+
[label setFont:[UIFont systemFontOfSize:gFontSize]];
200+
[label setTextColor:[UIColor blackColor]];
201+
}
202+
count += 1;
203+
}
204+
205+
if (expanded) {
206+
[[self layer] setFrame:[self frameShrunk]];
207+
[UIView commitAnimations];
208+
[self setExpanded:NO];
209+
}
210+
211+
if (_selectedItem != selectedItem) {
212+
_selectedItem = selectedItem;
213+
[self sendActionsForControlEvents:UIControlEventValueChanged];
214+
}
215+
}
216+
}
217+
218+
- (void)dealloc {
219+
[self setTitleLabel:nil];
220+
[self setLabels:nil];
221+
[super dealloc];
222+
}
223+
224+
225+
@end

Classes/ViewController.h

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
//
2+
// ViewController.h
3+
// Created by http://github.com/iosdeveloper
4+
//
5+
6+
#import <UIKit/UIKit.h>
7+
#import <AVFoundation/AVFoundation.h>
8+
9+
@interface ViewController : UIViewController
10+
11+
@end

Classes/ViewController.m

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
//
2+
// ViewController.m
3+
// Created by http://github.com/iosdeveloper
4+
//
5+
6+
#import "ViewController.h"
7+
#import "ExpandyButton.h"
8+
9+
@implementation ViewController
10+
11+
- (void)toggleFlashlight:(id)sender {
12+
AVCaptureDevice *device = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
13+
14+
[device lockForConfiguration:nil];
15+
[device setTorchMode:[(ExpandyButton *)sender selectedItem]];
16+
[device unlockForConfiguration];
17+
}
18+
19+
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
20+
- (void)viewDidLoad {
21+
[super viewDidLoad];
22+
23+
[[self view] setBackgroundColor:[UIColor blackColor]];
24+
25+
AVCaptureDevice *device = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
26+
27+
if ([device hasTorch]) {
28+
ExpandyButton *torchModeButton = [[[ExpandyButton alloc] initWithPoint:CGPointMake(20.0, 20.0)
29+
title:@"Flash"
30+
buttonNames:[NSArray arrayWithObjects:@"Off", @"On", @"Auto", nil]
31+
selectedItem:[device torchMode]] autorelease];
32+
[torchModeButton addTarget:self action:@selector(toggleFlashlight:) forControlEvents:UIControlEventValueChanged];
33+
[torchModeButton setHidden:NO];
34+
35+
[[self view] addSubview:torchModeButton];
36+
}
37+
}
38+
39+
@end

ExpandyButton-Info.plist

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
3+
<plist version="1.0">
4+
<dict>
5+
<key>CFBundleDevelopmentRegion</key>
6+
<string>English</string>
7+
<key>CFBundleDisplayName</key>
8+
<string>${PRODUCT_NAME}</string>
9+
<key>CFBundleExecutable</key>
10+
<string>${EXECUTABLE_NAME}</string>
11+
<key>CFBundleIconFile</key>
12+
<string></string>
13+
<key>CFBundleIdentifier</key>
14+
<string>com.github.iosdeveloper.expandybutton</string>
15+
<key>CFBundleInfoDictionaryVersion</key>
16+
<string>6.0</string>
17+
<key>CFBundleName</key>
18+
<string>${PRODUCT_NAME}</string>
19+
<key>CFBundlePackageType</key>
20+
<string>APPL</string>
21+
<key>CFBundleSignature</key>
22+
<string>????</string>
23+
<key>CFBundleVersion</key>
24+
<string>1.0</string>
25+
<key>LSRequiresIPhoneOS</key>
26+
<true/>
27+
<key>NSMainNibFile</key>
28+
<string>MainWindow</string>
29+
</dict>
30+
</plist>

0 commit comments

Comments
 (0)