forked from tidev/titanium-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLauncherView.m
970 lines (831 loc) · 24.4 KB
/
LauncherView.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
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
/**
* Appcelerator Titanium Mobile
* Copyright (c) 2009-2010 by Appcelerator, Inc. All Rights Reserved.
* Licensed under the terms of the Apache Public License
* Please see the LICENSE included with this distribution for details.
*/
// A good bit of this code was derived from the Three20 project
// and was customized to work inside Titanium
//
// All modifications by Appcelerator are licensed under
// the Apache License, Version 2.0
//
//
// Copyright 2009 Facebook
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
#ifdef USE_TI_UIDASHBOARDVIEW
#ifndef TI_INVALIDATE_TIMER
#define TI_INVALIDATE_TIMER(__TIMER) { [__TIMER invalidate]; __TIMER = nil; }
#endif
#import "LauncherView.h"
#import "LauncherItem.h"
#import "LauncherButton.h"
#import "TiBase.h"
static const CGFloat kLauncherViewMargin = 0;
static const CGFloat kLauncherViewPadding = 0;
static const CGFloat kLauncherViewPagerHeight = 20;
static const CGFloat kLauncherViewWobbleRadians = 1.5;
static const CGFloat kLauncherViewSpringLoadFraction = 0.18;
static const NSTimeInterval kLauncherViewEditHoldTimeInterval = 1;
static const NSTimeInterval kLauncherViewSpringLoadTimeInterval = 0.5;
static const NSTimeInterval kLauncherViewWobbleTime = 0.07;
static const NSInteger kLauncherViewDefaultColumnCount = 3;
static const NSTimeInterval kLauncherViewTransitionDuration = 0.3;
static const NSTimeInterval kLauncherViewFastTransitionDuration = 0.2;
@interface LauncherScrollView : UIScrollView
@end
@implementation LauncherScrollView
- (BOOL)touchesShouldCancelInContentView:(UIView *)view {
return !self.delaysContentTouches;
}
@end
@implementation LauncherView
@synthesize columnCount, rowCount, delegate, editable;
- (id)initWithFrame:(CGRect)frame withRowCount:(int)newRowCount withColumnCount:(int)newColumnCount
{
if ((self = [super initWithFrame:frame]))
{
self.rowCount = newRowCount;
self.columnCount = newColumnCount;
self.currentPageIndex = 0;
self.editable = YES;
renderingButtons = NO;
scrollView = [[LauncherScrollView alloc] initWithFrame:CGRectMake(0, 0, frame.size.width, frame.size.height - kLauncherViewPagerHeight - 30)];
scrollView.delegate = self;
scrollView.scrollsToTop = NO;
scrollView.showsVerticalScrollIndicator = NO;
scrollView.showsHorizontalScrollIndicator = NO;
scrollView.alwaysBounceHorizontal = YES;
scrollView.pagingEnabled = YES;
scrollView.autoresizingMask = UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleHeight;
scrollView.delaysContentTouches = NO;
scrollView.multipleTouchEnabled = NO;
[self addSubview:scrollView];
pager = [[UIPageControl alloc] init];
pager.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin|UIViewAutoresizingFlexibleRightMargin|UIViewAutoresizingFlexibleBottomMargin;
[pager addTarget:self action:@selector(pageChanged) forControlEvents:UIControlEventValueChanged];
[self addSubview:pager];
}
return self;
}
- (void)dealloc
{
delegate = nil;
if (editing)
{
[self endEditing];
}
[pager release];
[buttons release];
[scrollView release];
[pages release];
[super dealloc];
}
-(LauncherButton*)addButtonWithItem:(LauncherItem*)item
{
LauncherButton *button = [[LauncherButton alloc] initWithFrame:CGRectZero];
[button addTarget:self action:@selector(buttonTouchedUpInside:) forControlEvents:UIControlEventTouchUpInside];
[button addTarget:self action:@selector(buttonTouchedUpOutside:) forControlEvents:UIControlEventTouchUpOutside];
[button addTarget:self action:@selector(buttonTouchedDown:withEvent:) forControlEvents:UIControlEventTouchDown];
[scrollView addSubview:button];
button.item = item;
return [button autorelease];
}
-(NSInteger)rowHeight
{
return MAX(33,(scrollView.frame.size.height / rowCount));
}
- (NSMutableArray*)pageWithFreeSpace:(NSInteger)pageIndex
{
for (NSInteger i = self.currentPageIndex; i < pages.count; ++i)
{
NSMutableArray* page = [pages objectAtIndex:i];
if (page.count < self.columnCount*self.rowCount)
{
return page;
}
}
NSMutableArray* page = [NSMutableArray array];
[pages addObject:page];
return page;
}
- (NSInteger)currentPageIndex
{
return floor(scrollView.contentOffset.x/scrollView.frame.size.width);
}
- (NSMutableArray*)pageWithItem:(LauncherItem*)item
{
for (NSMutableArray* page in pages)
{
NSUInteger itemIndex = [page indexOfObject:item];
if (itemIndex != NSNotFound)
{
return page;
}
}
return nil;
}
- (NSIndexPath*)indexPathOfItem:(LauncherItem*)item
{
for (NSUInteger pageIndex = 0; pageIndex < pages.count; ++pageIndex)
{
NSArray* page = [pages objectAtIndex:pageIndex];
NSUInteger itemIndex = [page indexOfObject:item];
if (itemIndex != NSNotFound)
{
NSUInteger path[] = {pageIndex, itemIndex};
return [NSIndexPath indexPathWithIndexes:path length:2];
}
}
return nil;
}
- (NSMutableArray*)pageWithButton:(LauncherButton*)button
{
NSIndexPath* path = [self indexPathOfItem:button.item];
if (path)
{
NSInteger pageIndex = [path indexAtPosition:0];
return [buttons objectAtIndex:pageIndex];
}
return nil;
}
- (void)setCurrentPageIndex:(NSInteger)pageIndex
{
scrollView.contentOffset = CGPointMake(scrollView.frame.size.width*pageIndex, 0);
}
- (void)updateContentSize:(NSInteger)numberOfPages
{
scrollView.contentSize = CGSizeMake(numberOfPages*scrollView.frame.size.width, scrollView.frame.size.height);
if (numberOfPages != pager.numberOfPages)
{
pager.numberOfPages = numberOfPages;
}
}
- (void)layoutButtons
{
[self layoutIfNeeded];
CGFloat buttonWidth = ceil((self.frame.size.width - (kLauncherViewMargin*2 + kLauncherViewPadding*(self.columnCount-1))) / self.columnCount);
CGFloat buttonHeight = [self rowHeight];
CGFloat pageWidth = scrollView.frame.size.width;
CGFloat x = kLauncherViewMargin, minX = 0;
for (NSMutableArray* buttonPage in buttons)
{
CGFloat y = kLauncherViewMargin;
for (LauncherButton* button in buttonPage)
{
CGRect frame = CGRectMake(x, y, buttonWidth, buttonHeight);
if (!button.dragging)
{
button.transform = CGAffineTransformIdentity;
button.frame = button == dragButton ? [scrollView convertRect:frame toView:self] : frame;
}
x += buttonWidth + kLauncherViewPadding;
if (x >= minX+pageWidth)
{
y += buttonHeight + kLauncherViewPadding;
x = minX+kLauncherViewMargin;
}
}
minX += pageWidth;
x = minX;
}
NSInteger numberOfPages = pages.count;
[self updateContentSize:numberOfPages];
}
- (void)recreateButtons
{
if (![NSThread isMainThread]) {
TiThreadPerformOnMainThread( ^{
[self recreateButtons];
}, NO);
return;
}
renderingButtons = YES;
[self layoutIfNeeded];
NSInteger curIndex = self.currentPageIndex;
for (UIView *view in [scrollView subviews])
{
[view removeFromSuperview];
}
[buttons release];
buttons = [[NSMutableArray alloc] init];
for (NSArray* page in pages)
{
NSMutableArray* buttonPage = [NSMutableArray array];
[buttons addObject:buttonPage];
for (LauncherItem* item in page)
{
LauncherButton* button = [self addButtonWithItem:item];
[buttonPage addObject:button];
}
}
renderingButtons = NO;
[self layoutButtons];
NSInteger oldPageNo = pager.currentPage;
[pager setCurrentPage:curIndex];
if (oldPageNo != curIndex) {
if ([delegate respondsToSelector:@selector(launcherView:didChangePage:)]) {
[delegate launcherView:self didChangePage:[NSNumber numberWithInteger:pager.currentPage]];
}
}
}
- (void)scrollToItem:(LauncherItem*)item animated:(BOOL)animated
{
NSIndexPath* path = [self indexPathOfItem:item];
if (path!=nil)
{
NSUInteger page = [path indexAtPosition:0];
CGFloat x = page * scrollView.frame.size.width;
[scrollView setContentOffset:CGPointMake(x, 0) animated:animated];
}
}
- (void)addItem:(LauncherItem*)item animated:(BOOL)animated
{
if (pages==nil)
{
pages = [[NSMutableArray arrayWithObject:[NSMutableArray arrayWithObject:item]] retain];
}
else
{
NSMutableArray* page = [self pageWithFreeSpace:self.currentPageIndex];
[page addObject:item];
}
if ([delegate respondsToSelector:@selector(launcherView:didAddItem:)])
{
[delegate launcherView:self didAddItem:item];
}
if (buttons)
{
[self recreateButtons];
}
}
- (void)layoutSubviews
{
[super layoutSubviews];
pager.frame = CGRectMake(0, scrollView.frame.size.height, self.frame.size.width, kLauncherViewPagerHeight);
if (buttons==nil && !renderingButtons)
{
[self recreateButtons];
}
}
- (void)startDraggingButton:(LauncherButton*)button withEvent:(UIEvent*)event
{
TI_INVALIDATE_TIMER(springLoadTimer);
if (button)
{
if ([delegate respondsToSelector:@selector(launcherView:willDragItem:)]) {
[delegate launcherView:self willDragItem:button.item];
}
button.transform = CGAffineTransformIdentity;
[self addSubview:button];
CGPoint point = [scrollView convertPoint:button.frame.origin toView:self];
button.frame = CGRectMake(point.x, point.y, button.frame.size.width, button.frame.size.height);
[button layoutIfNeeded];
}
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:kLauncherViewFastTransitionDuration];
if (dragButton)
{
[dragButton setSelected:NO];
[dragButton setHighlighted:NO];
dragButton.dragging = NO;
[self layoutButtons];
}
if (button)
{
dragButton = button;
NSIndexPath* indexPath = [self indexPathOfItem:button.item];
positionOrigin = [indexPath indexAtPosition:1];
UITouch* touch = [[event allTouches] anyObject];
touchOrigin = [touch locationInView:scrollView];
dragOrigin = button.center;
dragTouch = touch;
button.dragging = YES;
scrollView.scrollEnabled = NO;
}
else
{
[UIView setAnimationDelegate:self];
[UIView setAnimationDidStopSelector:@selector(releaseButtonDidStop)];
scrollView.scrollEnabled = YES;
}
[UIView commitAnimations];
}
- (void)releaseButtonDidStop
{
[scrollView addSubview:dragButton];
CGPoint point = [self convertPoint:dragButton.frame.origin toView:scrollView];
dragButton.frame = CGRectMake(point.x, point.y, dragButton.frame.size.width, dragButton.frame.size.height);
dragButton = nil;
}
- (void)deselectButton:(LauncherButton*)button
{
[button setSelected:NO];
}
- (void)buttonTouchedUpInside:(LauncherButton*)button
{
if (editing)
{
if (button == dragButton)
{
[self startDraggingButton:nil withEvent:nil];
}
}
else
{
TI_INVALIDATE_TIMER(editHoldTimer);
[button setSelected:YES];
[self performSelector:@selector(deselectButton:) withObject:button afterDelay:kLauncherViewTransitionDuration];
if ([delegate respondsToSelector:@selector(launcherView:didSelectItem:)])
{
[delegate launcherView:self didSelectItem:button.item];
}
}
}
- (void)buttonTouchedUpOutside:(LauncherButton*)button
{
if (editing)
{
if (button == dragButton)
{
[self startDraggingButton:nil withEvent:nil];
}
}
else
{
TI_INVALIDATE_TIMER(editHoldTimer);
}
}
- (void)wobble
{
static BOOL wobblesLeft = NO;
if (editing)
{
CGFloat rotation = (kLauncherViewWobbleRadians * M_PI) / 180.0;
CGAffineTransform wobbleLeft = CGAffineTransformMakeRotation(rotation);
CGAffineTransform wobbleRight = CGAffineTransformMakeRotation(-rotation);
[UIView beginAnimations:nil context:nil];
NSInteger i = 0;
NSInteger nWobblyButtons = 0;
for (NSArray* buttonPage in buttons)
{
for (LauncherButton* button in buttonPage)
{
if (button != dragButton)
{
++nWobblyButtons;
if (i % 2)
{
button.transform = wobblesLeft ? wobbleRight : wobbleLeft;
}
else
{
button.transform = wobblesLeft ? wobbleLeft : wobbleRight;
}
}
++i;
}
}
if (nWobblyButtons >= 1)
{
[UIView setAnimationDuration:kLauncherViewWobbleTime];
[UIView setAnimationDelegate:self];
[UIView setAnimationDidStopSelector:@selector(wobble)];
wobblesLeft = !wobblesLeft;
}
else
{
[NSObject cancelPreviousPerformRequestsWithTarget:self];
[self performSelector:@selector(wobble) withObject:nil afterDelay:kLauncherViewWobbleTime];
}
[UIView commitAnimations];
}
}
- (void)endEditingAnimationDidStop:(NSString*)animationID finished:(NSNumber*)finished
context:(void*)context
{
for (NSArray* buttonPage in buttons)
{
for (LauncherButton* button in buttonPage)
{
button.editing = NO;
}
}
}
- (LauncherButton*)buttonForItem:(LauncherItem*)item
{
NSIndexPath* path = [self indexPathOfItem:item];
if (path)
{
NSInteger pageIndex = [path indexAtPosition:0];
NSArray* buttonPage = [buttons objectAtIndex:pageIndex];
NSInteger itemIndex = [path indexAtPosition:1];
return [buttonPage objectAtIndex:itemIndex];
}
return nil;
}
- (void)removeItem:(LauncherItem*)item animated:(BOOL)animated
{
NSMutableArray* itemPage = [self pageWithItem:item];
if (itemPage)
{
LauncherButton* button = [self buttonForItem:item];
NSMutableArray* buttonPage = [self pageWithButton:button];
[itemPage removeObject:button.item];
if (buttonPage)
{
[buttonPage removeObject:button];
if (animated)
{
[UIView beginAnimations:nil context:button];
[UIView setAnimationDuration:kLauncherViewFastTransitionDuration];
[UIView setAnimationDelegate:self];
[UIView setAnimationDidStopSelector:@selector(removeButtonAnimationDidStop:finished:context:)];
[self layoutButtons];
button.transform = CGAffineTransformMakeScale(0.01, 0.01);
button.alpha = 0;
[UIView commitAnimations];
}
else
{
[button removeFromSuperview];
[self layoutButtons];
}
}
if ([delegate respondsToSelector:@selector(launcherView:didRemoveItem:)])
{
[delegate launcherView:self didRemoveItem:item];
}
}
}
- (void)closeButtonTouchedUpInside:(LauncherButton*)closeButton
{
for (NSArray* buttonPage in buttons)
{
for (LauncherButton* button in buttonPage)
{
if (button.closeButton == closeButton)
{
[self removeItem:button.item animated:YES];
return;
}
}
}
}
- (NSArray*)launcheritems_
{
NSMutableArray *items = [NSMutableArray array];
for (NSArray* buttonPage in buttons)
{
for (LauncherButton* button in buttonPage)
{
[items addObject:button.item];
}
}
return items;
}
- (NSArray*)items
{
NSMutableArray *items = [NSMutableArray array];
for (NSArray* buttonPage in buttons)
{
for (LauncherButton* button in buttonPage)
{
[items addObject:button.item.userData];
}
}
return items;
}
- (LauncherItem*)itemForIndex:(NSInteger)index
{
NSInteger c = 0;
for (NSArray* buttonPage in buttons)
{
for (LauncherButton* button in buttonPage)
{
if (c == index)
{
return button.item;
}
c++;
}
}
return nil;
}
- (void)beginEditing
{
editing = YES;
scrollView.delaysContentTouches = YES;
for (NSArray* buttonPage in buttons)
{
for (LauncherButton* button in buttonPage)
{
button.editing = YES;
[button.closeButton addTarget:self action:@selector(closeButtonTouchedUpInside:) forControlEvents:UIControlEventTouchUpInside];
}
}
// Add a page at the end
[pages addObject:[NSMutableArray array]];
[buttons addObject:[NSMutableArray array]];
[self updateContentSize:pages.count];
BOOL shouldWobble = YES;
if ([delegate respondsToSelector:@selector(launcherViewShouldWobble:)])
{
shouldWobble = [delegate launcherViewShouldWobble:self];
}
if (shouldWobble)
{
[self wobble];
}
if ([delegate respondsToSelector:@selector(launcherViewDidBeginEditing:)])
{
[delegate launcherViewDidBeginEditing:self];
}
}
- (void)endEditing
{
editing = NO;
scrollView.delaysContentTouches = NO;
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:kLauncherViewTransitionDuration];
[UIView setAnimationDelegate:self];
[UIView setAnimationDidStopSelector:@selector(endEditingAnimationDidStop:finished:context:)];
for (NSArray* buttonPage in buttons)
{
for (LauncherButton* button in buttonPage)
{
button.transform = CGAffineTransformIdentity;
button.closeButton.alpha = 0;
}
}
[UIView commitAnimations];
NSInteger curIndex = self.currentPageIndex;
for (NSInteger i = 0; i < pages.count; ++i)
{
NSArray* page = [pages objectAtIndex:i];
if (!page.count)
{
[pages removeObjectAtIndex:i];
[buttons removeObjectAtIndex:i];
--i;
}
}
[self layoutButtons];
NSInteger oldPageNo = pager.currentPage;
[pager setCurrentPage:curIndex];
if (oldPageNo != curIndex) {
if ([delegate respondsToSelector:@selector(launcherView:didChangePage:)]) {
[delegate launcherView:self didChangePage:[NSNumber numberWithInteger:pager.currentPage]];
}
}
if ([delegate respondsToSelector:@selector(launcherViewDidEndEditing:)])
{
[delegate launcherViewDidEndEditing:self];
}
}
- (void)editHoldTimer:(NSTimer*)timer
{
editHoldTimer = nil;
NSArray *data = timer.userInfo;
LauncherButton *button = [data objectAtIndex:0];
UIEvent *event = [data objectAtIndex:1];
if ( button.item.userData == nil) {
return;
}
[self beginEditing];
[button setSelected:NO];
[button setHighlighted:NO];
[self startDraggingButton:button withEvent:event];
}
- (void)buttonTouchedDown:(LauncherButton*)button withEvent:(UIEvent*)event
{
if (editing)
{
if (!dragButton)
{
[self startDraggingButton:button withEvent:event];
}
}
else
{
TI_INVALIDATE_TIMER(editHoldTimer);
if (editable) {
editHoldTimer = [NSTimer scheduledTimerWithTimeInterval:kLauncherViewEditHoldTimeInterval
target:self selector:@selector(editHoldTimer:)
userInfo:[NSArray arrayWithObjects:button,event,nil]
repeats:NO];
}
}
}
- (void)checkButtonOverflow:(NSInteger)pageIndex
{
NSMutableArray* buttonPage = [buttons objectAtIndex:pageIndex];
NSInteger maxButtonsPerPage = self.columnCount*self.rowCount;
if (buttonPage.count > maxButtonsPerPage)
{
BOOL isLastPage = pageIndex == [buttons count]-1;
NSMutableArray* itemsPage = [pages objectAtIndex:pageIndex];
NSMutableArray* nextButtonPage = nil;
NSMutableArray* nextItemsPage = nil;
if (isLastPage)
{
nextButtonPage = [NSMutableArray array];
[buttons addObject:nextButtonPage];
nextItemsPage = [NSMutableArray array];
[pages addObject:nextItemsPage];
}
else
{
nextButtonPage = [buttons objectAtIndex:pageIndex+1];
nextItemsPage = [pages objectAtIndex:pageIndex+1];
}
while (buttonPage.count > maxButtonsPerPage)
{
[nextButtonPage insertObject:[buttonPage lastObject] atIndex:0];
[buttonPage removeLastObject];
[nextItemsPage insertObject:[itemsPage lastObject] atIndex:0];
[itemsPage removeLastObject];
}
if (pageIndex+1 < [buttons count])
{
[self checkButtonOverflow:pageIndex+1];
}
}
}
- (void)updatePagerWithContentOffset:(CGPoint)contentOffset
{
CGFloat pageWidth = scrollView.frame.size.width;
NSInteger oldPageNo = pager.currentPage;
pager.currentPage = floor((contentOffset.x - pageWidth / 2) / pageWidth) + 1;
if (oldPageNo != pager.currentPage) {
if ([delegate respondsToSelector:@selector(launcherView:didChangePage:)]) {
[delegate launcherView:self didChangePage:[NSNumber numberWithInteger:pager.currentPage]];
}
}
}
- (void)springLoadTimer:(NSTimer*)timer
{
springLoadTimer = nil;
if ([(NSNumber*)timer.userInfo boolValue])
{
CGFloat newX = scrollView.contentOffset.x - scrollView.frame.size.width;
if (newX >= 0)
{
CGPoint offset = CGPointMake(newX, 0);
[scrollView setContentOffset:offset animated:YES];
[self updatePagerWithContentOffset:offset];
dragOrigin.x += scrollView.frame.size.width;
positionOrigin = -1;
springing = YES;
[self performSelector:@selector(springingDidStop) withObject:nil afterDelay:0.3];
}
}
else
{
CGFloat newX = scrollView.contentOffset.x + scrollView.frame.size.width;
if (newX <= scrollView.contentSize.width - scrollView.frame.size.width)
{
CGPoint offset = CGPointMake(newX, 0);
[scrollView setContentOffset:offset animated:YES];
[self updatePagerWithContentOffset:offset];
dragOrigin.x -= scrollView.frame.size.width;
positionOrigin = -1;
springing = YES;
[self performSelector:@selector(springingDidStop) withObject:nil afterDelay:0.3];
}
}
}
- (void)springingDidStop
{
springing = NO;
}
- (void)updateTouch
{
CGPoint origin = [dragTouch locationInView:scrollView];
dragButton.center = CGPointMake(dragOrigin.x + (origin.x - touchOrigin.x),
dragOrigin.y + (origin.y - touchOrigin.y));
CGFloat x = origin.x - scrollView.contentOffset.x;
NSInteger column = round(x/dragButton.frame.size.width);
NSInteger row = round(origin.y/dragButton.frame.size.height);
NSInteger itemIndex = (row * self.columnCount) + column;
NSInteger pageIndex = MAX(floor(scrollView.contentOffset.x/scrollView.frame.size.width),0);
if (itemIndex != positionOrigin)
{
NSMutableArray* currentButtonPage = [buttons objectAtIndex:pageIndex];
if (itemIndex > currentButtonPage.count)
{
itemIndex = currentButtonPage.count;
}
if (itemIndex != positionOrigin)
{
[[dragButton retain] autorelease];
NSMutableArray* itemPage = [self pageWithItem:dragButton.item];
NSMutableArray* buttonPage = [self pageWithButton:dragButton];
[itemPage removeObject:dragButton.item];
[buttonPage removeObject:dragButton];
if (itemIndex > currentButtonPage.count)
{
itemIndex = currentButtonPage.count;
}
BOOL didMove = itemIndex != positionOrigin;
NSMutableArray* currentItemPage = [pages objectAtIndex:pageIndex];
[currentItemPage insertObject:dragButton.item atIndex:itemIndex];
[currentButtonPage insertObject:dragButton atIndex:itemIndex];
positionOrigin = itemIndex;
[self checkButtonOverflow:pageIndex];
if (didMove)
{
if ([delegate respondsToSelector:@selector(launcherView:didMoveItem:)])
{
[delegate launcherView:self didMoveItem:dragButton.item];
}
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:kLauncherViewTransitionDuration];
[self layoutButtons];
[UIView commitAnimations];
}
}
}
CGFloat springLoadDistance = dragButton.frame.size.width*kLauncherViewSpringLoadFraction;
BOOL goToPreviousPage = dragButton.center.x - springLoadDistance < 0;
BOOL goToNextPage = ((scrollView.frame.size.width - dragButton.center.x) - springLoadDistance) < 0;
if (goToPreviousPage || goToNextPage)
{
if (!springLoadTimer)
{
springLoadTimer = [NSTimer scheduledTimerWithTimeInterval:kLauncherViewSpringLoadTimeInterval
target:self selector:@selector(springLoadTimer:)
userInfo:[NSNumber numberWithBool:goToPreviousPage] repeats:NO];
}
}
else
{
TI_INVALIDATE_TIMER(springLoadTimer);
}
}
- (void)touchesMoved:(NSSet*)touches withEvent:(UIEvent *)event
{
[super touchesMoved:touches withEvent:event];
if (dragButton && !springing)
{
for (UITouch* touch in touches)
{
if (touch == dragTouch)
{
[self updateTouch];
break;
}
}
}
}
- (void)touchesEnded:(NSSet*)touches withEvent:(UIEvent *)event
{
[super touchesEnded:touches withEvent:event];
if (dragTouch)
{
for (UITouch* touch in touches)
{
if (touch == dragTouch)
{
dragTouch = nil;
if ([delegate respondsToSelector:@selector(launcherView:didDragItem:)]) {
[delegate launcherView:self didDragItem:dragButton.item];
}
break;
}
}
}
}
- (BOOL)editing
{
return editing;
}
- (void)pageChanged
{
if ([delegate respondsToSelector:@selector(launcherView:didChangePage:)]) {
[delegate launcherView:self didChangePage:[NSNumber numberWithInteger:pager.currentPage]];
}
scrollView.contentOffset = CGPointMake(pager.currentPage * scrollView.frame.size.width, 0);
}
- (void)scrollViewWillBeginDragging:(UIScrollView*)scrollView
{
TI_INVALIDATE_TIMER(editHoldTimer);
}
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView_
{
[self updatePagerWithContentOffset:scrollView.contentOffset];
}
@end
#endif