Advertise here




Advertise here

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Sign In with Google Sign In with OpenID
Please do not post the same thing multiple times. The board software automatically flags certain posts as needing moderator attention. This happens the most often for new users. I'm pretty sure this is made clear at the time you attempt to post. Posting the same thing over and over again just makes that many more posts the moderators have to weed through later. This makes us sad. Don't make us sad. If your post/thread doesn't appear, just wait a while. Don't post it again. If it hasn't shown up by the next day, then you can try again. I normally go through posts in the mornings, and try to check a few times throughout the day, but I'm not here 24/7. There will typically be a significant delay before posts are approved. Just be patient.

[cocos2d] CCItemsScroller - another items scroller

camka-devcamka-dev Posts: 39Registered Users
Hey all.

here is my implementation of CCItemsScroller, it can scroll items horizontally or vertically, just look at the sample.

developed with Cocos2d 2.0 rc2

header:
//
// CCItemsScroller.h
//
// Created by Aleksander Bykin on 26.06.12.
// Copyright 2012. All rights reserved.
//

#import \"cocos2d.h\"

typedef enum
{
CCItemsScrollerVertical,
CCItemsScrollerHorizontal
} CCItemsScrollerOrientations;

@protocol CCItemsScrollerDelegate;

@interface CCItemsScroller : CCLayer

@property (strong, nonatomic) id<CCItemsScrollerDelegate> delegate;
@property (strong, nonatomic) NSMutableArray *items;
@property (assign, nonatomic) CCItemsScrollerOrientations orientation;

+(id)itemsScrollerWithItems:(NSArray*)items andOrientation:(CCItemsScrollerOrientations)orientation andRect:(CGRect)rect;

-(id)initWithItems:(NSArray*)items andOrientation:(CCItemsScrollerOrientations)orientation andRect:(CGRect)rect;

@end

// PROTOCOL
@protocol CCItemsScrollerDelegate <NSObject>

@optional

- (void)itemsScrollerScrollingStarted:(CCItemsScroller *)sender;

- (void)itemsScroller:(CCItemsScroller *)sender didScrollToItemNumber:(int)index;

@end


implementation:
//
// CCItemsScroller.m
//
// Created by Aleksander Bykin on 26.06.12.
// Copyright 2012. All rights reserved.
//

#import \"CCItemsScroller.h\"

enum
{
kCCScrollLayerStateIdle,
kCCScrollLayerStateSliding
};

@implementation CCItemsScroller{
CGRect _rect;
int _state;
CGFloat _startSwipe;
CGFloat _offsetX;
CGFloat _offsetY;
CGSize _itemSize;
CGRect _glRect;
}

@synthesize delegate = _delegate;
@synthesize items = _items;
@synthesize orientation = _orientation;

+(id)itemsScrollerWithItems:(NSArray *)items andOrientation:(CCItemsScrollerOrientations)orientation andRect:(CGRect)rect{
return [[self alloc] initWithItems:items andOrientation:(CCItemsScrollerOrientations)orientation andRect:rect];
}

-(id)initWithItems:(NSArray *)items andOrientation:(CCItemsScrollerOrientations)orientation andRect:(CGRect)rect{
self = [super init];

if(self){
_items = [NSMutableArray arrayWithArray:items];
_rect = rect;
_orientation = orientation;

_glRect = CC_RECT_POINTS_TO_PIXELS(_rect);

self.isTouchEnabled = YES;
[self updateItems];
}

return self;
}

-(void)updateItems{
int i = 0;
CGFloat x = 0;
CGFloat y = 0;
for (CCLayer *item in _items)
{
if(i == 0){
int csWidth = 0;
int csHeight = 0;

_itemSize = CGSizeMake(item.contentSize.width, item.contentSize.height);

if(_orientation == CCItemsScrollerHorizontal){
csWidth = _items.count*_itemSize.width;
csHeight = _rect.size.height;
}

if(_orientation == CCItemsScrollerVertical){
csWidth = _rect.size.width;
csHeight = _items.count*_itemSize.height;
}

self.contentSize = CGSizeMake(csWidth, csHeight);
}

if (_orientation == CCItemsScrollerHorizontal) {
x = (i * item.contentSize.width);
}

if(_orientation == CCItemsScrollerVertical){
y = (i * item.contentSize.height);
}

item.position = ccp(x, y);

if (!item.parent)
[self addChild:item];

++i;
}

_offsetX = _rect.origin.x;
_offsetY = _rect.origin.y;

if(_orientation == CCItemsScrollerHorizontal)
self.position = ccp(_rect.origin.x, _rect.origin.y);

if(_orientation == CCItemsScrollerVertical){
_offsetY = -(self.contentSize.height-_rect.size.height-_rect.origin.y);
self.position = ccp(_rect.origin.x, _offsetY);
}
}

-(void) visit
{
//glPushMatrix();
glEnable(GL_SCISSOR_TEST);
glScissor(_glRect.origin.x, _glRect.origin.y, _glRect.size.width, _glRect.size.height);

[super visit];

glDisable(GL_SCISSOR_TEST);
//glPopMatrix();
}

-(void) registerWithTouchDispatcher
{
CCTouchDispatcher *dispatcher = [[CCDirector sharedDirector] touchDispatcher];
int priority = kCCMenuHandlerPriority - 1;

[dispatcher addTargetedDelegate:self priority:priority swallowsTouches:NO];
}

-(BOOL) ccTouchBegan:(UITouch *)touch withEvent:(UIEvent *)event
{
_state = kCCScrollLayerStateIdle;
return YES;
}

- (void)ccTouchMoved:(UITouch *)touch withEvent:(UIEvent *)event
{
CGPoint touchPoint = [touch locationInView:[touch view]];
touchPoint = [[CCDirector sharedDirector] convertToGL:touchPoint];

if ( _state != kCCScrollLayerStateSliding )
{
_state = kCCScrollLayerStateSliding;

if(_orientation == CCItemsScrollerHorizontal){
_startSwipe = _offsetX - touchPoint.x;
}

if(_orientation == CCItemsScrollerVertical){
_startSwipe = _offsetY - touchPoint.y;
}

//if ([self.delegate respondsToSelector:@selector(scrollLayerScrollingStarted:)])
// [self.delegate scrollLayerScrollingStarted: self];
}

if (_state == kCCScrollLayerStateSliding)
{
if(_orientation == CCItemsScrollerHorizontal){
_offsetX = _startSwipe + touchPoint.x;
if(_startSwipe < touchPoint.x){
if(_offsetX > _rect.origin.x){
_offsetX = _rect.origin.x;
}
else{
if(_offsetX < -(self.contentSize.width-_rect.size.width-_rect.origin.x))
_offsetX = -(self.contentSize.width-_rect.size.width-_rect.origin.x);
}
}
else{
if((self.contentSize.width+_offsetX-_rect.origin.x) < _rect.size.width){
_offsetX = _rect.size.width + _rect.origin.x - self.contentSize.width;
}
}
}

if(_orientation == CCItemsScrollerVertical){
_offsetY = _startSwipe + touchPoint.y;

if(_startSwipe < touchPoint.y){
if (_offsetY > _rect.origin.y) {
_offsetY = _rect.origin.y;
}else
if (_offsetY < -(self.contentSize.height-_rect.size.height-_rect.origin.y))
{
_offsetY = -(self.contentSize.height-_rect.size.height-_rect.origin.y);
}
}
else {
if (_offsetY > _rect.origin.y) {
_offsetY = _rect.origin.y;
}
}
}

self.position = ccp(_offsetX, _offsetY);
}
}

@end


>>> here is sample of usage <<<</a>

p.s. code writed with ARC
Post edited by camka-dev on
Make the world better

Replies

Sign In or Register to comment.