iOS cocos2d实现自定义button(按钮特效)控件效果源码

6 篇文章 0 订阅
6 篇文章 0 订阅

开发人员:Jason's.Alex   QQ:531401335

csdn博客:http://blog.csdn.net/RuShrooM

//
//  SpriteMenu.h
//  DiceGameBox2D
//
//  Created by 电脑 富力 on 12-12-25.
//  Copyright (c) 2012年 科技. All rights reserved.
//菜单精灵

//开发人员:Jason's.Alex
//QQ:531401335

#import "cocos2d.h"
#import "ResourceLoad.h"

@interface SpriteMenu : CCSprite<CCTargetedTouchDelegate>
{
    CCSprite *normalImage;//没有选择的图片
    CCSprite *selImage;   //选着的图片
    
    id target; //调用对象
    SEL endedFunAddr; //触摸结束调用的函数
    SEL beganFunAddr;//触摸开始调用的函数
    
    BOOL isTouchEffect;//触摸特效
    
    float zoom;//缩放
}

@property(nonatomic,readwrite,assign)  CCSprite *normalImage;//没有选择的图片
@property(nonatomic,readwrite,assign)  CCSprite *selImage;   //选着的图片
@property(readwrite)BOOL isTouchEffect;

+(id)menuWithSpriteMenu:(id)imagePath SelImage:(id)selPath target:(id)tag  beganFunc:(SEL)beganFunc  endedFunc:(SEL)endedFunc;

-(id)initWithSpriteMenu:(id)imagePath SelImage:(id)selPath target:(id)tag  beganFunc:(SEL)beganFunc endedFunc:(SEL)endedFunc;

-(void)showNormalImage;
-(void)ShowSelectImage;



-(void)showEffect:(NSString*)name;//显示特效

@end

//
//  SpriteMenu.m
//  DiceGameBox2D
//
//  Created by 电脑 富力 on 12-12-25.
//  Copyright (c) 2012年 科技. All rights reserved.
//
//开发人员:Jason's.Alex
//QQ:531401335

#import "SpriteMenu.h"

@implementation SpriteMenu

@synthesize normalImage;
@synthesize selImage;
@synthesize isTouchEffect;

+(id)menuWithSpriteMenu:(id)imagePath SelImage:(id)selPath target:(id)tag  beganFunc:(SEL)beganFunc  endedFunc:(SEL)endedFunc;
{
    return [[[self alloc] initWithSpriteMenu:imagePath SelImage:selPath target:tag beganFunc:beganFunc endedFunc:endedFunc]autorelease];
}

-(id)initWithSpriteMenu:(id)imagePath SelImage:(id)selPath target:(id)tag  beganFunc:(SEL)beganFunc endedFunc:(SEL)endedFunc;
{
    if(self=[super init])
    {
                
        normalImage=[[CCSprite alloc]initWithFile:imagePath];
        selImage=[[CCSprite alloc]initWithFile:selPath];
        
        target=tag;
        endedFunAddr=endedFunc;
        beganFunAddr=beganFunc;
        
        [self setDisplayFrame:[normalImage displayedFrame]];
        isTouchEffect=FALSE;//默认启用触摸特效的
        zoom=[self scale];
    }
    
    return self;
}

-(void)dealloc
{
    NSLog(@"~SpriteMenu");
    [normalImage release];
    [selImage release];
    
    [super dealloc];
}


-(void)onEnter
{
    [[CCTouchDispatcher sharedDispatcher] addTargetedDelegate:self priority:10 swallowsTouches:YES];
    
    [super onEnter];
}

-(void)onExit
{
    [[CCTouchDispatcher sharedDispatcher]removeDelegate:self];

    [super onExit];
}

-(BOOL)ccTouchBegan:(UITouch *)touch withEvent:(UIEvent *)event
{
    CGPoint touchLocal=[touch locationInView:[touch view]];
    touchLocal=[[CCDirector sharedDirector]convertToGL:touchLocal];
    
    BOOL isTouch=CGRectContainsPoint([self boundingBox], touchLocal);
    if(isTouch)
    {
        [self ShowSelectImage];
            
            if(isTouchEffect) //是否使用了触摸特效
            {
                id scaleTo=[CCScaleTo actionWithDuration:0.25f scale:zoom-0.2f];
                id callfunc=nil;
                
                if(beganFunAddr!=nil)
                    callfunc=[CCCallFunc actionWithTarget:target selector:beganFunAddr];
                
                id sequece=[CCSequence actions:scaleTo, callfunc,nil];
                [self runAction:sequece];
            
            }else
            {
                if(beganFunAddr!=nil)
                    [target performSelector:beganFunAddr];
            }

    }
    
    return isTouch;
}


-(void)ccTouchMoved:(UITouch *)touch withEvent:(UIEvent *)event
{

}

-(void)ccTouchEnded:(UITouch *)touch withEvent:(UIEvent *)event
{
    [self showNormalImage];
    
    if(isTouchEffect)
    {
        id scaleTo=[CCScaleTo actionWithDuration:0.25f scale:zoom+0.2f];
        id scaleTo1=[CCScaleTo actionWithDuration:0.25f scale:zoom];
        
        id callfunc=nil;
        
        if(endedFunAddr!=nil)
            callfunc=[CCCallFunc actionWithTarget:target selector:endedFunAddr];
        
        CCSequence* sequece=[CCSequence actions:scaleTo,scaleTo1,callfunc,nil];
        
        [self runAction:sequece];
    }else
    {
        if(endedFunAddr!=nil)
            [target performSelector:endedFunAddr];
    }

}


-(void)showNormalImage
{
    [self setDisplayFrame:[normalImage displayedFrame]];
}

-(void)ShowSelectImage
{
    [self setDisplayFrame:[selImage displayedFrame]];
}

-(void)showEffect:(NSString*)name//显示特效
{
    if([name isEqualToString:@"stretch"])//拉伸特效
    {
        [self setScaleX:0];
        id scaleXTo=[CCScaleTo actionWithDuration:2.0f scale:zoom];//拉伸
        id fadeIn=[CCFadeIn actionWithDuration:2.0f];
        
        id spawn=[CCSpawn actions:scaleXTo,fadeIn,nil];
        
        [self runAction:spawn];
    }
}

@end


  • 3
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值