|
| 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 |
0 commit comments