Skip to content

Commit 18e9829

Browse files
committed
Adds CCScrollViewDelegate. Only implemented recognition MAC platform
Former-commit-id: b451463
1 parent 3e9dc2c commit 18e9829

File tree

2 files changed

+88
-26
lines changed

2 files changed

+88
-26
lines changed

cocos2d-ui/CCScrollView.h

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,18 @@
2525
#import "CCNode.h"
2626

2727
@class CCTapDownGestureRecognizer;
28+
@class CCScrollView;
29+
30+
@protocol CCScrollViewDelegate <NSObject>
31+
32+
@optional
33+
- (void)scrollViewDidScroll:(CCScrollView *)scrollView;
34+
- (void)scrollViewWillBeginDragging:(CCScrollView *)scrollView;
35+
- (void)scrollViewDidEndDragging:(CCScrollView * )scrollView willDecelerate:(BOOL)decelerate;
36+
- (void)scrollViewWillBeginDecelerating:(CCScrollView *)scrollView;
37+
- (void)scrollViewDidEndDecelerating:(CCScrollView *)scrollView;
38+
39+
@end
2840

2941
#ifdef __CC_PLATFORM_IOS
3042

@@ -52,6 +64,8 @@
5264
CGPoint _velocity;
5365
}
5466

67+
@property (nonatomic, weak) id<CCScrollViewDelegate> delegate;
68+
5569
@property (nonatomic,strong) CCNode* contentNode;
5670

5771
@property (nonatomic,assign) BOOL flipYCoordinates;

cocos2d-ui/CCScrollView.m

Lines changed: 74 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -335,31 +335,13 @@ - (void) setScrollPosition:(CGPoint)newPos
335335

336336
- (void) setScrollPosition:(CGPoint)newPos animated:(BOOL)animated
337337
{
338+
// Check bounds
339+
newPos.x = MAX(MIN(newPos.x, self.maxScrollX), self.minScrollX);
340+
newPos.y = MAX(MIN(newPos.y, self.maxScrollY), self.minScrollY);
341+
338342
BOOL xMoved = (newPos.x != self.scrollPosition.x);
339343
BOOL yMoved = (newPos.y != self.scrollPosition.y);
340-
341-
// Check bounds
342-
if (newPos.x > self.maxScrollX)
343-
{
344-
newPos.x = self.maxScrollX;
345-
xMoved = YES;
346-
}
347-
if (newPos.x < self.minScrollX)
348-
{
349-
newPos.x = self.minScrollX;
350-
xMoved = YES;
351-
}
352-
if (newPos.y > self.maxScrollY)
353-
{
354-
newPos.y = self.maxScrollY;
355-
yMoved = YES;
356-
}
357-
if (newPos.y < self.minScrollY)
358-
{
359-
newPos.y = self.minScrollY;
360-
yMoved = YES;
361-
}
362-
344+
363345
if (animated)
364346
{
365347
CGPoint oldPos = self.scrollPosition;
@@ -749,11 +731,35 @@ - (void) onExitTransitionDidStart
749731

750732
- (void)scrollWheel:(NSEvent *)theEvent
751733
{
752-
CCDirector* dir = [CCDirector sharedDirector];
753-
734+
CCDirector* dir = [CCDirector sharedDirector];
735+
754736
float deltaX = theEvent.deltaX;
755737
float deltaY = theEvent.deltaY;
756-
738+
739+
[self scrollViewDidScroll];
740+
741+
switch (theEvent.phase) {
742+
case NSEventPhaseBegan:
743+
[self scrollViewWillBeginDragging];
744+
break;
745+
case NSEventPhaseEnded:
746+
//TODO: add logic to determine if it will decelerate
747+
[self scrollViewDidEndDraggingAndWillDecelerate:YES];
748+
default:
749+
break;
750+
}
751+
752+
switch (theEvent.momentumPhase) {
753+
case NSEventPhaseBegan:
754+
[self scrollViewWillBeginDecelerating];
755+
break;
756+
case NSEventPhaseEnded:
757+
[self scrollViewDidEndDecelerating];
758+
default:
759+
break;
760+
}
761+
762+
757763
// Calculate the delta in node space
758764
CGPoint ref = [dir convertToGL:CGPointZero];
759765
ref = [self convertToNodeSpace:ref];
@@ -824,4 +830,46 @@ - (void)scrollWheel:(NSEvent *)theEvent
824830

825831
#endif
826832

833+
834+
#pragma mark - CCScrollViewDelegate Helpers
835+
836+
- (void)scrollViewDidScroll
837+
{
838+
if ( [self.delegate respondsToSelector:@selector(scrollViewDidScroll:)] )
839+
{
840+
[self.delegate scrollViewDidScroll:self];
841+
}
842+
}
843+
844+
- (void)scrollViewWillBeginDragging
845+
{
846+
if ( [self.delegate respondsToSelector:@selector(scrollViewWillBeginDragging:)])
847+
{
848+
[self.delegate scrollViewWillBeginDragging:self];
849+
}
850+
}
851+
- (void)scrollViewDidEndDraggingAndWillDecelerate:(BOOL)decelerate
852+
{
853+
if ([self.delegate respondsToSelector:@selector(scrollViewDidEndDragging:willDecelerate:)])
854+
{
855+
[self.delegate scrollViewDidEndDragging:self
856+
willDecelerate:decelerate];
857+
}
858+
}
859+
- (void)scrollViewWillBeginDecelerating
860+
{
861+
if ( [self.delegate respondsToSelector:@selector(scrollViewWillBeginDecelerating:)])
862+
{
863+
[self.delegate scrollViewWillBeginDecelerating:self];
864+
}
865+
866+
}
867+
- (void)scrollViewDidEndDecelerating
868+
{
869+
if ( [self.delegate respondsToSelector:@selector(scrollViewDidEndDecelerating:)])
870+
{
871+
[self.delegate scrollViewDidEndDecelerating:self];
872+
}
873+
}
874+
827875
@end

0 commit comments

Comments
 (0)