Skip to content

Commit 6f3382c

Browse files
committed
Adds iOS callbacks for CCScrollViewDelegate + Fixes scrollViewDidEndDecelerating for mac
Former-commit-id: 0ca53b2
1 parent 18e9829 commit 6f3382c

File tree

2 files changed

+91
-24
lines changed

2 files changed

+91
-24
lines changed

cocos2d-ui-tests/tests/CCScrollViewTest.m

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424

2525
#import "TestBase.h"
2626

27-
@interface CCScrollViewTest : TestBase @end
27+
@interface CCScrollViewTest : TestBase <CCScrollViewDelegate> @end
2828

2929
@implementation CCScrollViewTest
3030

@@ -46,10 +46,36 @@ - (void)setupScrollViewBasicTest
4646

4747
CCScrollView* scrollView = [[CCScrollView alloc] initWithContentNode:[self createScrollContent]];
4848
scrollView.flipYCoordinates = NO;
49+
scrollView.delegate = self;
4950

5051
[self.contentNode addChild:scrollView];
5152
}
5253

54+
- (void)scrollViewDidEndDecelerating:(CCScrollView *)scrollView
55+
{
56+
CCLOG(@"did end decelerating");
57+
}
58+
59+
- (void)scrollViewDidEndDragging:(CCScrollView *)scrollView willDecelerate:(BOOL)decelerate
60+
{
61+
CCLOG(@"did end dragging, decelerate: %@", decelerate ? @"YES" : @"NO");
62+
}
63+
64+
- (void)scrollViewWillBeginDecelerating:(CCScrollView *)scrollView
65+
{
66+
CCLOG(@"will begin decelerating");
67+
}
68+
69+
- (void)scrollViewWillBeginDragging:(CCScrollView *)scrollView
70+
{
71+
CCLOG(@"will begin dragging");
72+
}
73+
74+
- (void)scrollViewDidScroll:(CCScrollView *)scrollView
75+
{
76+
//CCLOG(@"Scrolling");
77+
}
78+
5379
- (void)setupScrollViewPagingTest
5480
{
5581
self.subTitle = @"Paging - Pan the content layer it should snap into a 3 x 3 grid.";

cocos2d-ui/CCScrollView.m

Lines changed: 64 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -91,15 +91,18 @@ @interface CCMoveToX : CCActionInterval
9191
{
9292
float _endPosition;
9393
float _startPos;
94+
void (^block)(void);
9495
}
9596
@end
9697

9798
@implementation CCMoveToX
9899

99-
-(id) initWithDuration: (CCTime) t positionX: (float) p
100+
-(id) initWithDuration: (CCTime) t positionX: (float) p callback:(void(^)(void))callback
100101
{
101-
if( (self=[super initWithDuration: t]) )
102+
if( (self=[super initWithDuration: t]) ) {
102103
_endPosition = p;
104+
block = callback;
105+
}
103106
return self;
104107
}
105108

@@ -119,6 +122,7 @@ -(void) update: (CCTime) t
119122
float y = node.position.y;
120123

121124
node.position = ccp(x,y);
125+
block();
122126
}
123127
@end
124128

@@ -127,15 +131,18 @@ @interface CCMoveToY : CCActionInterval
127131
{
128132
float _endPosition;
129133
float _startPos;
134+
void (^block)(void);
130135
}
131136
@end
132137

133138
@implementation CCMoveToY
134139

135-
-(id) initWithDuration: (CCTime) t positionY: (float) p
140+
-(id) initWithDuration: (CCTime) t positionY: (float) p callback:(void(^)(void))callback
136141
{
137-
if( (self=[super initWithDuration: t]) )
142+
if( (self=[super initWithDuration: t]) ) {
138143
_endPosition = p;
144+
block = [callback copy];
145+
}
139146
return self;
140147
}
141148

@@ -155,14 +162,21 @@ -(void) update: (CCTime) t
155162
float x = node.position.x;
156163

157164
node.position = ccp(x,y);
165+
block();
158166
}
159167
@end
160168

161169

162170
#pragma mark -
163171
#pragma mark CCScrollView
164172

165-
@implementation CCScrollView
173+
@implementation CCScrollView {
174+
BOOL _decelerating;
175+
176+
#ifdef __CC_PLATFORM_MAC
177+
CGPoint _lastPosition;
178+
#endif
179+
}
166180

167181
#pragma mark Initializers
168182

@@ -357,7 +371,9 @@ - (void) setScrollPosition:(CGPoint)newPos animated:(BOOL)animated
357371
_animatingX = YES;
358372

359373
// 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];
361377
CCActionCallFunc* callFunc = [CCActionCallFunc actionWithTarget:self selector:@selector(xAnimationDone)];
362378
action = [CCActionSequence actions:action, callFunc, nil];
363379
action.tag = kCCScrollViewActionXTag;
@@ -371,7 +387,9 @@ - (void) setScrollPosition:(CGPoint)newPos animated:(BOOL)animated
371387
_animatingY = YES;
372388

373389
// 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];
375393
CCActionCallFunc* callFunc = [CCActionCallFunc actionWithTarget:self selector:@selector(yAnimationDone)];
376394
action = [CCActionSequence actions:action, callFunc, nil];
377395
action.tag = kCCScrollViewActionYTag;
@@ -381,6 +399,9 @@ - (void) setScrollPosition:(CGPoint)newPos animated:(BOOL)animated
381399
}
382400
else
383401
{
402+
#ifdef __CC_PLATFORM_MAC
403+
_lastPosition = self.scrollPosition;
404+
#endif
384405
[_contentNode stopActionByTag:kCCScrollViewActionXTag];
385406
[_contentNode stopActionByTag:kCCScrollViewActionYTag];
386407
_contentNode.position = ccpMult(newPos, -1);
@@ -435,15 +456,32 @@ - (void) panLayerToTarget:(CGPoint) newPos
435456
if (newPos.y > self.maxScrollY) newPos.y = self.maxScrollY;
436457
if (newPos.y < self.minScrollY) newPos.y = self.minScrollY;
437458
}
438-
459+
[self scrollViewDidScroll];
439460
_contentNode.position = ccpMult(newPos, -1);
440461
}
441462

442463
- (void) update:(CCTime)df
443464
{
444465
float fps = 1.0/df;
445466
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+
447485
if (!_isPanning)
448486
{
449487
if (_velocity.x != 0 || _velocity.y != 0)
@@ -535,6 +573,7 @@ - (void)handlePan:(UIGestureRecognizer *)gestureRecognizer
535573

536574
if (pgr.state == UIGestureRecognizerStateBegan)
537575
{
576+
[self scrollViewWillBeginDragging];
538577
_animatingX = NO;
539578
_animatingY = NO;
540579
_rawTranslationStart = rawTranslation;
@@ -563,6 +602,7 @@ - (void)handlePan:(UIGestureRecognizer *)gestureRecognizer
563602
}
564603
else if (pgr.state == UIGestureRecognizerStateEnded)
565604
{
605+
566606
// Calculate the velocity in node space
567607
CGPoint ref = [dir convertToGL:CGPointZero];
568608
ref = [self convertToNodeSpace:ref];
@@ -577,6 +617,7 @@ - (void)handlePan:(UIGestureRecognizer *)gestureRecognizer
577617
// Check if scroll directions has been disabled
578618
if (!_horizontalScrollEnabled) _velocity.x = 0;
579619
if (!_verticalScrollEnabled) _velocity.y = 0;
620+
[self scrollViewDidEndDraggingAndWillDecelerate:!CGPointEqualToPoint(_velocity, CGPointZero)];
580621

581622
// Setup a target if paging is enabled
582623
if (_pagingEnabled)
@@ -615,7 +656,8 @@ - (void)handlePan:(UIGestureRecognizer *)gestureRecognizer
615656

616657
_velocity = CGPointZero;
617658
}
618-
659+
[self scrollViewWillBeginDecelerating];
660+
_decelerating = YES;
619661
_isPanning = NO;
620662
}
621663
else if (pgr.state == UIGestureRecognizerStateCancelled)
@@ -745,21 +787,16 @@ - (void)scrollWheel:(NSEvent *)theEvent
745787
case NSEventPhaseEnded:
746788
//TODO: add logic to determine if it will decelerate
747789
[self scrollViewDidEndDraggingAndWillDecelerate:YES];
790+
_decelerating = YES;
748791
default:
749792
break;
750793
}
751794

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];
760798
}
761799

762-
763800
// Calculate the delta in node space
764801
CGPoint ref = [dir convertToGL:CGPointZero];
765802
ref = [self convertToNodeSpace:ref];
@@ -774,6 +811,7 @@ - (void)scrollWheel:(NSEvent *)theEvent
774811
// Flip coordinates
775812
if (_flipYCoordinates) delta.y = -delta.y;
776813
delta.x = -delta.x;
814+
777815

778816
// Handle disabled x/y axis
779817
if (!_horizontalScrollEnabled) delta.x = 0;
@@ -824,7 +862,7 @@ - (void)scrollWheel:(NSEvent *)theEvent
824862
// Update scroll position
825863
CGPoint scrollPos = self.scrollPosition;
826864
scrollPos = ccpAdd(delta, scrollPos);
827-
self.scrollPosition = scrollPos;
865+
self.scrollPosition = scrollPos;
828866
}
829867
}
830868

@@ -858,10 +896,13 @@ - (void)scrollViewDidEndDraggingAndWillDecelerate:(BOOL)decelerate
858896
}
859897
- (void)scrollViewWillBeginDecelerating
860898
{
861-
if ( [self.delegate respondsToSelector:@selector(scrollViewWillBeginDecelerating:)])
899+
if ( !_pagingEnabled )
862900
{
863-
[self.delegate scrollViewWillBeginDecelerating:self];
864-
}
901+
if ( [self.delegate respondsToSelector:@selector(scrollViewWillBeginDecelerating:)])
902+
{
903+
[self.delegate scrollViewWillBeginDecelerating:self];
904+
}
905+
}
865906

866907
}
867908
- (void)scrollViewDidEndDecelerating

0 commit comments

Comments
 (0)