注册 登录
主题 : cocos2d-iphone looping menu (迴轉菜單??)
级别: 新手上路
UID: 11471
精华: 0
发帖: 4
可可豆: 25 CB
威望: 25 点
在线时间: 1(时)
注册时间: 2009-12-09
最后登录: 2010-04-24
0 楼:  发表于: 2010-01-04 17:11    发自: Web Page
来源于 iPhone类代码 分类

cocos2d-iphone looping menu (迴轉菜單??)    (在iOS代码库中浏览本帖)

最近在cocos2d-iphone 上看到的 looping menu
稍加修改後可以在新的sdk 上跑
在這跟大家分享一下
原始文章在這:
http://www.cocos2d-iphone.org/forum/topic/139



// LoopingMenu.h

#import 
#import "cocos2d.h"

@interface LoopingMenu : CCMenu
{    
    bool moving;
    float hPadding;
    float lowerBound;
}
@end

// InputController.h

#import 
#import "cocos2d.h"

// 跟原始碼不同的地方,在這裡我用单例模式

@interface InputController :NSObject{

}
+ (InputController*)sharedInstance;

-(CGPoint) rotateRealWorld:(CGPoint) location;
-(BOOL)wasSwipeLeft:(NSSet *)touches :(UIEvent*)event;
-(BOOL)wasSwipeRight:(NSSet*)touches :(UIEvent*) event;
-(CGPoint)getDistance:(int)finger :(NSSet*)touches :(UIEvent*) event;
@end

// LoopingMenu.m

#import "LoopingMenu.h"
#import "InputController.h"


@interface CCMenu (Private)
// returns touched menu item, if any, implemented in Menu.m
-(CCMenuItem *) itemForTouch: (UITouch *) touch;
@end

@interface LoopingMenu(Animation)
-(void) updateAnimation;
@end

@implementation LoopingMenu

#pragma mark -
#pragma mark CCMenu

// 垂直的沒有效果
-(void) alignItemsVerticallyWithPadding:(float)padding
{
    [self alignItemsHorizontallyWithPadding:padding];
}

-(void) alignItemsHorizontallyWithPadding:(float)padding
{
    hPadding = padding;
    lowerBound = [(CCMenuItem*)[children objectAtIndex:0] contentSize].height / 2.0;
    [super alignItemsHorizontallyWithPadding:padding];
    [self updateAnimation];
}

-(void) registerWithTouchDispatcher
{
    [[CCTouchDispatcher sharedDispatcher] addTargetedDelegate:self
                                                   priority:INT_MIN+1 swallowsTouches:false];
}

#pragma mark -
#pragma mark Touches

-(BOOL) ccTouchBegan:(UITouch *)touch withEvent:(UIEvent *)event
{
    if ([[event allTouches] count] != 1)
        return FALSE;
    
    moving = FALSE;
    selectedItem = [super itemForTouch:touch];
    [selectedItem selected];
    
    state = kMenuStateTrackingTouch;
    return TRUE;
}

-(void) ccTouchEnded:(UITouch *)touch withEvent:(UIEvent *)event
{
    if ([[event allTouches] count] != 1)
    {
        [self ccTouchCancelled:touch withEvent:event];
        return;
    }
    
    if(!moving)
        [super ccTouchEnded:touch withEvent:event];
    else
        [super ccTouchCancelled:touch withEvent:event];
    moving = FALSE;
}

-(void) ccTouchCancelled:(UITouch *)touch withEvent:(UIEvent *)event
{
    [selectedItem unselected];
    state = kMenuStateWaiting;
    moving = FALSE;
}

-(void) ccTouchMoved:(UITouch *)touch withEvent:(UIEvent *)event
{
    if ([[event allTouches] count] != 1)
    {
        [self ccTouchCancelled:touch withEvent:event];
        return;
    }
    
    NSMutableSet* touches = [[[NSMutableSet alloc] initWithObjects:touch, nil]
                             autorelease];
    
    CGPoint distance = [[InputController sharedInstance] getDistance:1:touches :event];
    
    if([[InputController sharedInstance] wasSwipeLeft:touches :event] && distance.y < distance.x)
    {
        moving = TRUE;
        [selectedItem unselected];
        [self setPosition:ccpAdd([self position], ccp(-distance.x, 0))];
        
        CCMenuItem* leftItem = [children objectAtIndex:0];
        if([leftItem position].x + [self position].x + [leftItem
                                                        contentSize].width / 2.0  < 0)
        {
            [leftItem retain];
            [children removeObjectAtIndex:0];
            CCMenuItem* lastItem = [children objectAtIndex:[children count] - 1];
            [leftItem setPosition:ccpAdd([lastItem position], ccp([lastItem
                                                                   contentSize].width / 2.0 + [leftItem contentSize].width / 2.0 + hPadding, 0))];
            [children addObject:leftItem];
            [leftItem autorelease];
        }
    } 
    else if([[InputController sharedInstance] wasSwipeRight:touches:event] && distance.y <
            distance.x)
    {
        moving = TRUE;
        [selectedItem unselected];
        [self setPosition:ccpAdd([self position], ccp(distance.x, 0))];
        
        CCMenuItem* lastItem = [children objectAtIndex:[children count] - 1];
        if([lastItem position].x + [self position].x - [lastItem
                                                        contentSize].width / 2.0 > 480)
        {
            [lastItem retain];
            [children removeObjectAtIndex:[children count] - 1];
            CCMenuItem* firstItem = [children objectAtIndex:0];
            [lastItem setPosition:ccpSub([firstItem position], ccp([firstItem
                                                                    contentSize].width / 2.0 + [lastItem contentSize].width / 2.0 + hPadding, 0))];
            [children insertObject:lastItem atIndex:0];
            [lastItem autorelease];
        }
    }
    else if(!moving)
    {
        [super ccTouchMoved:touch withEvent:event];
    }
    
    [self updateAnimation];
}

@end

@implementation LoopingMenu(Animation)
// 所有的動畫效果都在這哩,可以依照自己的需求修改
-(void) updateAnimation
{
    static float quadraticCoefficient = -1.0/90000.0;
    
    for (CCMenuItem* item in children)
    {
        float distance = fabsf([item position].x - 240.0 + [self position].x);
        
        if (distance > 240.0)
            distance = 240.0;
        else if (distance < 0.0)
            distance = 0.0;
        float ratio = quadraticCoefficient * (distance*distance) + 1;
        
        [item setScale:ratio];
        [item setOpacity:ratio * 255.0];
        [item setPosition:ccp([item position].x, -(lowerBound - [item contentSize].height * ratio / 2.0))];
    }
}
@end

// 指列出這裡需要的
// InputController.m

#import "InputController.h"
    
@implementation InputController

static InputController *sharedInputController = nil;

+ (InputController*) sharedInstance
{    
    @synchronized(self)
    {
        if (!sharedInputController)
            sharedInputController  = [[InputController alloc] init];
    }
    return sharedInputController;
}

- (void) dealloc
{
    [super dealloc];
}

- (id) init
{
    self = [super self];
    if (self != nil)
    {
    }
    return self;
}

-(CGPoint) rotateRealWorld:(CGPoint) location
{
    return [[CCDirector sharedDirector] convertToUI:location];
}

-(BOOL)wasSwipeLeft:(NSSet *)touches :(UIEvent*)event
{
    NSSet *allTouches = [event allTouches];
    
    if(1 != [allTouches count])
        return FALSE;
    
    UITouch *touch = [[allTouches allObjects] objectAtIndex:0];
    
    CGPoint start = [touch previousLocationInView:[touch view]];
    CGPoint end = [touch locationInView:[touch view]];
    start = [self rotateRealWorld:start];
    end = [self rotateRealWorld:end];
    
    if(start.x > end.x)
    {
        return TRUE;
    }
    
    return FALSE;
}

-(BOOL)wasSwipeRight:(NSSet*)touches :(UIEvent*) event
{
    NSSet *allTouches = [event allTouches];
    
    if(1 != [allTouches count])
        return FALSE;
    
    UITouch *touch = [[allTouches allObjects] objectAtIndex:0];
    
    CGPoint start = [touch previousLocationInView:[touch view]];
    CGPoint end = [touch locationInView:[touch view]];
    start = [self rotateRealWorld:start];
    end = [self rotateRealWorld:end];
    
    if(start.x < end.x)
    {
        return TRUE;
    }
    
    return FALSE;
}

-(CGPoint)getDistance:(int)finger :(NSSet*)touches :(UIEvent*) event
{
    NSSet *allTouches = [event allTouches];
    
    if(finger == 0 || finger > [allTouches count])
        return CGPointZero;
    
    UITouch *touch = [[allTouches allObjects] objectAtIndex:finger - 1];
    
    CGPoint start1 = [touch previousLocationInView:[touch view]];
    CGPoint end1 = [touch locationInView:[touch view]];
    start1 = [self rotateRealWorld:start1];
    end1 = [self rotateRealWorld:end1];
    
    float xDistance = ( fabs(start1.x - end1.x));
    float yDistance =  ( fabs(start1.y - end1.y));
    return CGPointMake(xDistance, yDistance);
}
@end


级别: 骑士

UID: 47443
精华: 0
发帖: 274
可可豆: 857 CB
威望: 859 点
在线时间: 716(时)
注册时间: 2011-01-18
最后登录: 2019-01-18
1 楼:  发表于: 2011-07-20 09:34    发自: Web Page
it helps!有demo project就更好了
http://www.drvoice.cn 医声网
http://www.pace.org.cn 起搏在线
http://www.xlqianxian.com 小李嵌线
级别: 侠客
UID: 14458
精华: 0
发帖: 52
可可豆: 502 CB
威望: 502 点
在线时间: 258(时)
注册时间: 2010-02-24
最后登录: 2014-08-21
2 楼:  发表于: 2011-08-12 18:24    发自: Web Page
mark一下

CocoaChina社区转载内容已尽可能注明出处,如未能核实来源或转发内容图片有权利瑕疵的,请及时联系社区进行修改或删除【联系方式QQ : 3442093904 邮箱:support@cocoachina.com】文章内容为作者独立观点,不代表CocoaChina社区立场。版权归原作者所有,如申请授权请联系作者,因文章侵权CocoaChina社区不承担任何法律及连带责任。

描述
快速回复

关注本帖(如果有新回复会站内信通知您)

发帖、回帖都会得到可观的积分奖励。查看论坛积分规则

按"Ctrl+Enter"直接提交
    顶部