-
Notifications
You must be signed in to change notification settings - Fork 25
/
Copy pathRNExpandingButtonBar.m
410 lines (362 loc) · 15 KB
/
RNExpandingButtonBar.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
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
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
/* ---------------------------------------------------------
* ExpandingButtonBar
* Author: Ryan Nystrom
* Copyright (C) 2012 Ryan Nystrom
*
* 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.
*
* http://www.opensource.org/licenses/MIT
* -------------------------------------------------------*/
#import "ExpandingButtonBar.h"
//@interface ExpandingButton : UIButton
//
//@property (nonatomic, strong) UIView *test;
//
//@end
//
//@implementation ExpandingButton
//
//@synthesize test;
//
//-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
//{
// NSLog(@"touches began");
// [super touchesBegan:touches withEvent:event];
//}
//
//-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
//{
// NSLog(@"touches ended");
// [super touchesEnded:touches withEvent:event];
//}
//
//@end
@interface ExpandingButtonBar ()
- (void) _expand:(NSDictionary*)properties;
- (void) _close:(NSDictionary*)properties;
@end
@implementation ExpandingButtonBar
@synthesize buttons = _buttons;
@synthesize button = _button;
@synthesize toggledButton = _toggledButton;
@synthesize delegate = _delegate;
- (id) initWithImage:(UIImage*)image selectedImage:(UIImage*)selectedImage toggledImage:(UIImage*)toggledImage toggledSelectedImage:(UIImage*)toggledSelectedImage buttons:(NSArray*)buttons center:(CGPoint)center;
{
if (self = [super init]) {
[self setDefaults];
// Reverse buttons so it makes since for top/bottom
NSArray *reversedButtons = [[buttons reverseObjectEnumerator] allObjects];
[self setButtons:reversedButtons];
// Button location/size settings
CGRect buttonFrame = CGRectMake(0, 0, [image size].width, [image size].height);
CGPoint buttonCenter = CGPointMake([image size].width / 2.0f, [image size].height / 2.0f);
UIButton *defaultButton = [[UIButton alloc] initWithFrame:buttonFrame];
[defaultButton setCenter:buttonCenter];
[defaultButton setImage:image forState:UIControlStateNormal];
[defaultButton setImage:selectedImage forState:UIControlEventTouchDown];
[defaultButton addTarget:self action:@selector(onButton:) forControlEvents:UIControlEventTouchUpInside];
[self setButton:defaultButton];
UIButton *toggledButton = [[UIButton alloc] initWithFrame:buttonFrame];
[toggledButton setCenter:buttonCenter];
[toggledButton setImage:toggledImage forState:UIControlStateNormal];
[toggledButton setImage:toggledSelectedImage forState:UIControlEventTouchDown];
[toggledButton addTarget:self action:@selector(onToggledButton:) forControlEvents:UIControlEventTouchUpInside];
// Init invisible
[toggledButton setAlpha:0.0f];
[self setToggledButton:toggledButton];
for (int i = 0; i < [buttons count]; ++i) {
UIButton *button = (UIButton*)[buttons objectAtIndex:i];
[button addTarget:self action:@selector(explode:) forControlEvents:UIControlEventTouchUpInside];
[button setCenter:buttonCenter];
[button setAlpha:0.0f];
[self addSubview:button];
}
// Container view settings
[self setBackgroundColor:[UIColor clearColor]];
[self setFrame:buttonFrame];
[self setCenter:center];
[self addSubview:[self button]];
[self addSubview:[self toggledButton]];
}
return self;
}
- (void) setDefaults
{
_fadeTime = 0.2f;
_animationTime = 0.4f;
_padding = 15.0f;
_far = 15.0f;
_near = 7.0f;
_delay = 0.1f;
_toggled = NO;
_spin = NO;
_horizontal = NO;
_animated = YES;
}
- (void) onButton:(id)sender
{
[self showButtonsAnimated:_animated];
}
- (void) onToggledButton:(id)sender
{
[self hideButtonsAnimated:_animated];
}
- (void) showButtons
{
[self showButtonsAnimated:NO];
}
- (void) hideButtons
{
[self hideButtonsAnimated:NO];
}
- (void) toggleMainButton
{
UIButton *animateTo;
UIButton *animateFrom;
if (_toggled) {
animateTo = [self button];
animateFrom = [self toggledButton];
}
else {
animateTo = [self toggledButton];
animateFrom = [self button];
}
[UIView animateWithDuration:_fadeTime animations:^{
[animateTo setAlpha:1.0f];
[animateFrom setAlpha:0.0f];
}];
}
- (void) explode:(id)sender
{
if (! _explode) return;
UIView *view = (UIView*)sender;
CGAffineTransform scale = CGAffineTransformMakeScale(5.0f, 5.0f);
CGAffineTransform unScale = CGAffineTransformMakeScale(1.0f, 1.0f);
[UIView animateWithDuration:0.3 animations:^{
[view setAlpha:0.0f];
[view setTransform:scale];
} completion:^(BOOL finished){
[view setAlpha:1.0f];
[view setTransform:unScale];
}];
}
- (void) showButtonsAnimated:(BOOL)animated
{
if ([self delegate] && [[self delegate] respondsToSelector:@selector(expandingBarWillAppear:)]) {
[[self delegate] expandingBarWillAppear:self];
}
float y = [[self button] center].y;
float x = [[self button] center].x;
float endY = y;
float endX = x;
for (int i = 0; i < [[self buttons] count]; ++i) {
UIButton *button = [[self buttons] objectAtIndex:i];
endY -= [self getYoffset:button];
endX += [self getXoffset:button];
float farY = endY - ( ! _horizontal ? _far : 0.0f);
float farX = endX - (_horizontal ? _far : 0.0f);
float nearY = endY + ( ! _horizontal ? _near : 0.0f);
float nearX = endX + (_horizontal ? _near : 0.0f);
if (animated) {
NSMutableArray *animationOptions = [NSMutableArray array];
if (_spin) {
CAKeyframeAnimation *rotateAnimation = [CAKeyframeAnimation animationWithKeyPath:@"transform.rotation.z"];
[rotateAnimation setValues:[NSArray arrayWithObjects:[NSNumber numberWithFloat:0.0f],[NSNumber numberWithFloat:M_PI * 2], nil]];
[rotateAnimation setDuration:_animationTime];
[rotateAnimation setKeyTimes:[NSArray arrayWithObjects:[NSNumber numberWithFloat:0.0f], [NSNumber numberWithFloat:1.0f], nil]];
[animationOptions addObject:rotateAnimation];
}
CAKeyframeAnimation *positionAnimation = [CAKeyframeAnimation animationWithKeyPath:@"position"];
[positionAnimation setDuration:_animationTime];
CGMutablePathRef path = CGPathCreateMutable();
CGPathMoveToPoint(path, NULL, x, y);
CGPathAddLineToPoint(path, NULL, farX, farY);
CGPathAddLineToPoint(path, NULL, nearX, nearY);
CGPathAddLineToPoint(path, NULL, endX, endY);
[positionAnimation setPath: path];
CGPathRelease(path);
[animationOptions addObject:positionAnimation];
CAAnimationGroup *animationGroup = [CAAnimationGroup animation];
[animationGroup setAnimations: animationOptions];
[animationGroup setDuration:_animationTime];
[animationGroup setFillMode: kCAFillModeForwards];
[animationGroup setTimingFunction: [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseIn]];
NSDictionary *properties = [NSDictionary dictionaryWithObjects:[NSArray arrayWithObjects:button, [NSValue valueWithCGPoint:CGPointMake(endX, endY)], animationGroup, nil] forKeys:[NSArray arrayWithObjects:@"view", @"center", @"animation", nil]];
[self performSelector:@selector(_expand:) withObject:properties afterDelay:_delay * ([[self buttons] count] - i)];
}
else {
[button setCenter:CGPointMake(x, y)];
[button setAlpha:1.0f];
}
}
_toggled = NO;
[self toggleMainButton];
float delegateDelay = _animated ? [[self buttons] count] * _delay + _animationTime : 0.0f;
if ([self delegate] && [[self delegate] respondsToSelector:@selector(expandingBarDidAppear:)]) {
[[self delegate] performSelector:@selector(expandingBarDidAppear:) withObject:self afterDelay:delegateDelay];
}
}
- (void) hideButtonsAnimated:(BOOL)animated
{
if ([self delegate] && [[self delegate] respondsToSelector:@selector(expandingBarWillDisappear:)]) {
[[self delegate] performSelector:@selector(expandingBarWillDisappear:) withObject:self];
}
CGPoint center = [[self button] center];
float endY = center.y;
float endX = center.x;
for (int i = 0; i < [[self buttons] count]; ++i) {
UIButton *button = [[self buttons] objectAtIndex:i];
if (animated) {
NSMutableArray *animationOptions = [NSMutableArray array];
if (_spin) {
CAKeyframeAnimation *rotateAnimation = [CAKeyframeAnimation animationWithKeyPath:@"transform.rotation.z"];
[rotateAnimation setValues:[NSArray arrayWithObjects:[NSNumber numberWithFloat:0.0f],[NSNumber numberWithFloat:M_PI * -2], nil]];
[rotateAnimation setDuration:_animationTime];
[rotateAnimation setKeyTimes:[NSArray arrayWithObjects:[NSNumber numberWithFloat:0.0f], [NSNumber numberWithFloat:1.0f], nil]];
[animationOptions addObject:rotateAnimation];
}
CAKeyframeAnimation *opacityAnimation = [CAKeyframeAnimation animationWithKeyPath:@"opacity"];
[opacityAnimation setValues:[NSArray arrayWithObjects:[NSNumber numberWithFloat:1.0f], [NSNumber numberWithFloat:0.0f], nil]];
[opacityAnimation setDuration:_animationTime];
[animationOptions addObject:opacityAnimation];
float y = [button center].y;
float x = [button center].x;
CAKeyframeAnimation *positionAnimation = [CAKeyframeAnimation animationWithKeyPath:@"position"];
[positionAnimation setDuration:_animationTime];
CGMutablePathRef path = CGPathCreateMutable();
CGPathMoveToPoint(path, NULL, x, y);
CGPathAddLineToPoint(path, NULL, endX, endY);
[positionAnimation setPath: path];
CGPathRelease(path);
[animationOptions addObject:positionAnimation];
CAAnimationGroup *animationGroup = [CAAnimationGroup animation];
[animationGroup setAnimations: animationOptions];
[animationGroup setDuration:_animationTime];
[animationGroup setFillMode: kCAFillModeForwards];
[animationGroup setTimingFunction: [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseIn]];
NSDictionary *properties = [NSDictionary dictionaryWithObjects:[NSArray arrayWithObjects:button, animationGroup, nil] forKeys:[NSArray arrayWithObjects:@"view", @"animation", nil]];
[self performSelector:@selector(_close:) withObject:properties afterDelay:_delay * ([[self buttons] count] - i)];
}
else {
[button setCenter:center];
[button setAlpha:0.0f];
}
}
float delegateDelay = _animated ? [[self buttons] count] * _delay + _animationTime: 0.0f;
if ([self delegate] && [[self delegate] respondsToSelector:@selector(expandingBarDidDisappear:)]) {
[[self delegate] performSelector:@selector(expandingBarDidDisappear:) withObject:self afterDelay:delegateDelay];
}
_toggled = YES;
[self toggleMainButton];
}
- (void) _expand:(NSDictionary*)properties
{
UIView *view = [properties objectForKey:@"view"];
CAAnimationGroup *animationGroup = [properties objectForKey:@"animation"];
NSValue *val = [properties objectForKey:@"center"];
CGPoint center = [val CGPointValue];
[[view layer] addAnimation:animationGroup forKey:@"Expand"];
[view setCenter:center];
[view setAlpha:1.0f];
}
- (void) _close:(NSDictionary*)properties
{
UIView *view = [properties objectForKey:@"view"];
CAAnimationGroup *animationGroup = [properties objectForKey:@"animation"];
CGPoint center = [[self button] center];
[[view layer] addAnimation:animationGroup forKey:@"Collapse"];
[view setAlpha:0.0f];
[view setCenter:center];
}
- (int) getXoffset:(UIView*)view
{
if (_horizontal) {
return [view frame].size.height + _padding;
}
return 0;
}
- (int) getYoffset:(UIView*)view
{
if ( ! _horizontal) {
return [view frame].size.height + _padding;
}
return 0;
}
/* ----------------------------------------------
* You probably do not want to edit anything under here
* --------------------------------------------*/
- (void) setAnimationTime:(float)time
{
if (time > 0) {
_animationTime = time;
}
}
- (void) setFadeTime:(float)time
{
if (time > 0) {
_fadeTime = time;
}
}
- (void) setPadding:(float)padding
{
if (padding > 0) {
_padding = padding;
}
}
- (void) setSpin:(BOOL)b
{
_spin = b;
}
- (void) setHorizontal:(BOOL)b
{
NSArray *reversedButtons = [[[self buttons] reverseObjectEnumerator] allObjects];
[self setButtons:reversedButtons];
_horizontal = b;
}
- (void) setFar:(float)num
{
_far = num;
}
- (void) setNear:(float)num
{
_near = num;
}
- (void) setDelay:(float)num
{
_delay = num;
}
- (void) setExplode:(BOOL)b
{
_explode = b;
}
/* ----------------------------------------------
* DO NOT CHANGE
* The following is a hack to allow touches outside
* of this view. Use caution when changing.
* --------------------------------------------*/
- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event
{
UIView *v = nil;
v = [super hitTest:point withEvent:event];
return v;
}
- (BOOL)pointInside:(CGPoint)point withEvent:(UIEvent *)event
{
BOOL isInside = [super pointInside:point withEvent:event];
if (YES == isInside) {
return isInside;
}
for (UIButton *button in [self buttons]) {
CGPoint inButtonSpace = [self convertPoint:point toView:button];
BOOL isInsideButton = [button pointInside:inButtonSpace withEvent:nil];
if (YES == isInsideButton) {
return isInsideButton;
}
}
return isInside;
}
@end