@@ -91,15 +91,18 @@ @interface CCMoveToX : CCActionInterval
91
91
{
92
92
float _endPosition;
93
93
float _startPos;
94
+ void (^block)(void );
94
95
}
95
96
@end
96
97
97
98
@implementation CCMoveToX
98
99
99
- -(id ) initWithDuration : (CCTime) t positionX : (float ) p
100
+ -(id ) initWithDuration : (CCTime) t positionX : (float ) p callback : ( void (^)( void )) callback
100
101
{
101
- if ( (self=[super initWithDuration: t]) )
102
+ if ( (self=[super initWithDuration: t]) ) {
102
103
_endPosition = p;
104
+ block = callback;
105
+ }
103
106
return self;
104
107
}
105
108
@@ -119,6 +122,7 @@ -(void) update: (CCTime) t
119
122
float y = node.position .y ;
120
123
121
124
node.position = ccp (x,y);
125
+ block ();
122
126
}
123
127
@end
124
128
@@ -127,15 +131,18 @@ @interface CCMoveToY : CCActionInterval
127
131
{
128
132
float _endPosition;
129
133
float _startPos;
134
+ void (^block)(void );
130
135
}
131
136
@end
132
137
133
138
@implementation CCMoveToY
134
139
135
- -(id ) initWithDuration : (CCTime) t positionY : (float ) p
140
+ -(id ) initWithDuration : (CCTime) t positionY : (float ) p callback : ( void (^)( void )) callback
136
141
{
137
- if ( (self=[super initWithDuration: t]) )
142
+ if ( (self=[super initWithDuration: t]) ) {
138
143
_endPosition = p;
144
+ block = [callback copy ];
145
+ }
139
146
return self;
140
147
}
141
148
@@ -155,14 +162,21 @@ -(void) update: (CCTime) t
155
162
float x = node.position .x ;
156
163
157
164
node.position = ccp (x,y);
165
+ block ();
158
166
}
159
167
@end
160
168
161
169
162
170
#pragma mark -
163
171
#pragma mark CCScrollView
164
172
165
- @implementation CCScrollView
173
+ @implementation CCScrollView {
174
+ BOOL _decelerating;
175
+
176
+ #ifdef __CC_PLATFORM_MAC
177
+ CGPoint _lastPosition;
178
+ #endif
179
+ }
166
180
167
181
#pragma mark Initializers
168
182
@@ -357,7 +371,9 @@ - (void) setScrollPosition:(CGPoint)newPos animated:(BOOL)animated
357
371
_animatingX = YES ;
358
372
359
373
// Create animation action
360
- CCActionInterval* action = [CCActionEaseOut actionWithAction: [[CCMoveToX alloc ] initWithDuration: duration positionX: -newPos.x] rate: 2 ];
374
+ CCActionInterval* action = [CCActionEaseOut actionWithAction: [[CCMoveToX alloc ] initWithDuration: duration positionX: -newPos.x callback: ^{
375
+ [self scrollViewDidScroll ];
376
+ }] rate: 2 ];
361
377
CCActionCallFunc* callFunc = [CCActionCallFunc actionWithTarget: self selector: @selector (xAnimationDone )];
362
378
action = [CCActionSequence actions: action, callFunc, nil ];
363
379
action.tag = kCCScrollViewActionXTag ;
@@ -371,7 +387,9 @@ - (void) setScrollPosition:(CGPoint)newPos animated:(BOOL)animated
371
387
_animatingY = YES ;
372
388
373
389
// Create animation action
374
- CCActionInterval* action = [CCActionEaseOut actionWithAction: [[CCMoveToY alloc ] initWithDuration: duration positionY: -newPos.y] rate: 2 ];
390
+ CCActionInterval* action = [CCActionEaseOut actionWithAction: [[CCMoveToY alloc ] initWithDuration: duration positionY: -newPos.y callback: ^{
391
+ [self scrollViewDidScroll ];
392
+ }] rate: 2 ];
375
393
CCActionCallFunc* callFunc = [CCActionCallFunc actionWithTarget: self selector: @selector (yAnimationDone )];
376
394
action = [CCActionSequence actions: action, callFunc, nil ];
377
395
action.tag = kCCScrollViewActionYTag ;
@@ -381,6 +399,9 @@ - (void) setScrollPosition:(CGPoint)newPos animated:(BOOL)animated
381
399
}
382
400
else
383
401
{
402
+ #ifdef __CC_PLATFORM_MAC
403
+ _lastPosition = self.scrollPosition ;
404
+ #endif
384
405
[_contentNode stopActionByTag: kCCScrollViewActionXTag ];
385
406
[_contentNode stopActionByTag: kCCScrollViewActionYTag ];
386
407
_contentNode.position = ccpMult (newPos, -1 );
@@ -435,15 +456,32 @@ - (void) panLayerToTarget:(CGPoint) newPos
435
456
if (newPos.y > self.maxScrollY ) newPos.y = self.maxScrollY ;
436
457
if (newPos.y < self.minScrollY ) newPos.y = self.minScrollY ;
437
458
}
438
-
459
+ [ self scrollViewDidScroll ];
439
460
_contentNode.position = ccpMult (newPos, -1 );
440
461
}
441
462
442
463
- (void ) update : (CCTime)df
443
464
{
444
465
float fps = 1.0 /df;
445
466
float p = 60 /fps;
446
-
467
+
468
+ if (! CGPointEqualToPoint (_velocity, CGPointZero) ) {
469
+ [self scrollViewDidScroll ];
470
+ } else {
471
+
472
+ #ifdef __CC_PLATFORM_IOS
473
+ if ( _decelerating && !(_animatingX || _animatingY)) {
474
+ [self scrollViewDidEndDecelerating ];
475
+ _decelerating = NO ;
476
+ }
477
+ #elif defined(__CC_PLATFORM_MAC)
478
+ if ( _decelerating && CGPointEqualToPoint (_lastPosition, self.scrollPosition )) {
479
+ [self scrollViewDidEndDecelerating ];
480
+ _decelerating = NO ;
481
+ }
482
+ #endif
483
+ }
484
+
447
485
if (!_isPanning)
448
486
{
449
487
if (_velocity.x != 0 || _velocity.y != 0 )
@@ -535,6 +573,7 @@ - (void)handlePan:(UIGestureRecognizer *)gestureRecognizer
535
573
536
574
if (pgr.state == UIGestureRecognizerStateBegan)
537
575
{
576
+ [self scrollViewWillBeginDragging ];
538
577
_animatingX = NO ;
539
578
_animatingY = NO ;
540
579
_rawTranslationStart = rawTranslation;
@@ -563,6 +602,7 @@ - (void)handlePan:(UIGestureRecognizer *)gestureRecognizer
563
602
}
564
603
else if (pgr.state == UIGestureRecognizerStateEnded)
565
604
{
605
+
566
606
// Calculate the velocity in node space
567
607
CGPoint ref = [dir convertToGL: CGPointZero];
568
608
ref = [self convertToNodeSpace: ref];
@@ -577,6 +617,7 @@ - (void)handlePan:(UIGestureRecognizer *)gestureRecognizer
577
617
// Check if scroll directions has been disabled
578
618
if (!_horizontalScrollEnabled) _velocity.x = 0 ;
579
619
if (!_verticalScrollEnabled) _velocity.y = 0 ;
620
+ [self scrollViewDidEndDraggingAndWillDecelerate: !CGPointEqualToPoint (_velocity, CGPointZero)];
580
621
581
622
// Setup a target if paging is enabled
582
623
if (_pagingEnabled)
@@ -615,7 +656,8 @@ - (void)handlePan:(UIGestureRecognizer *)gestureRecognizer
615
656
616
657
_velocity = CGPointZero;
617
658
}
618
-
659
+ [self scrollViewWillBeginDecelerating ];
660
+ _decelerating = YES ;
619
661
_isPanning = NO ;
620
662
}
621
663
else if (pgr.state == UIGestureRecognizerStateCancelled)
@@ -745,21 +787,16 @@ - (void)scrollWheel:(NSEvent *)theEvent
745
787
case NSEventPhaseEnded:
746
788
// TODO: add logic to determine if it will decelerate
747
789
[self scrollViewDidEndDraggingAndWillDecelerate: YES ];
790
+ _decelerating = YES ;
748
791
default :
749
792
break ;
750
793
}
751
794
752
- switch (theEvent.momentumPhase ) {
753
- case NSEventPhaseBegan:
754
- [self scrollViewWillBeginDecelerating ];
755
- break ;
756
- case NSEventPhaseEnded:
757
- [self scrollViewDidEndDecelerating ];
758
- default :
759
- break ;
795
+ if (theEvent.momentumPhase == NSEventPhaseBegan)
796
+ {
797
+ [self scrollViewWillBeginDecelerating ];
760
798
}
761
799
762
-
763
800
// Calculate the delta in node space
764
801
CGPoint ref = [dir convertToGL: CGPointZero];
765
802
ref = [self convertToNodeSpace: ref];
@@ -774,6 +811,7 @@ - (void)scrollWheel:(NSEvent *)theEvent
774
811
// Flip coordinates
775
812
if (_flipYCoordinates) delta.y = -delta.y ;
776
813
delta.x = -delta.x ;
814
+
777
815
778
816
// Handle disabled x/y axis
779
817
if (!_horizontalScrollEnabled) delta.x = 0 ;
@@ -824,7 +862,7 @@ - (void)scrollWheel:(NSEvent *)theEvent
824
862
// Update scroll position
825
863
CGPoint scrollPos = self.scrollPosition ;
826
864
scrollPos = ccpAdd (delta, scrollPos);
827
- self.scrollPosition = scrollPos;
865
+ self.scrollPosition = scrollPos;
828
866
}
829
867
}
830
868
@@ -858,10 +896,13 @@ - (void)scrollViewDidEndDraggingAndWillDecelerate:(BOOL)decelerate
858
896
}
859
897
- (void )scrollViewWillBeginDecelerating
860
898
{
861
- if ( [ self .delegate respondsToSelector: @selector ( scrollViewWillBeginDecelerating: )] )
899
+ if ( !_pagingEnabled )
862
900
{
863
- [self .delegate scrollViewWillBeginDecelerating: self ];
864
- }
901
+ if ( [self .delegate respondsToSelector: @selector (scrollViewWillBeginDecelerating: )])
902
+ {
903
+ [self .delegate scrollViewWillBeginDecelerating: self ];
904
+ }
905
+ }
865
906
866
907
}
867
908
- (void )scrollViewDidEndDecelerating
0 commit comments