It looks like you're new here. If you want to get involved, click one of these buttons!
camka-dev
Posts: 39Registered Users
//
// 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
//
// 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
Replies
- now scroller support item selection;
project grow up, i cant publish here source, just take it from link below.
>>> HERE IS SAMPLE PROJECT <<<</a>
have a nice day :)
- Spam
- Abuse
- Troll
0 · Off Topic Insightful Disagree Dislike Like Awesome