iOS cocos2d实现自定义button(按钮特效)控件效果源码
2014-12-20 10:57
513 查看
这半年一直在用object-c开发一个ios游戏。使用cocos2d和box2d 。开发游戏变的简单多了。这游戏开发了半年多了。直到最近这个游戏停止了,因为资金问题,老大没法在发更多的工资了。哎,真的非常遗憾,我一个人完成游戏的编辑器开发,脚本开发,游戏代码开发,很不容易,因为我学object-c,coco2d才看了2个星期的书就直接开发了,以前是搞c++的吗。感觉4个人开发游戏真的很累,游戏为了脱颖出更加真实的效果还使用了物理引擎,在老大的同意的情况下,我共享cocos2d自己写的一些大家比较常用的,因为cocos2d有些控件不怎么好用或者没有,反正我觉得是这样的。如slider(滑块),button(按钮),RollNumber(数字滚动),Progress(进度条)....控件一一在我的博客里面公布,可以直接使用.源码打包下载
开发人员:Jason's.Alex QQ:531401335
csdn博客:http://blog.csdn.net/RuShrooM
[cpp]
view plaincopy
//
// 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
[cpp]
view plaincopy
//
// 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
开发人员:Jason's.Alex QQ:531401335
csdn博客:http://blog.csdn.net/RuShrooM
[cpp]
view plaincopy
//
// 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
[cpp]
view plaincopy
//
// 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
相关文章推荐
- iOS cocos2d实现自定义button(按钮特效)控件效果源码
- Android中ImageButton自定义按钮的按下效果的代码实现方法,附网上2种经典解决方法。
- Android中为Button自定义按钮的按下效果的实现方法
- 史上最简单的自定义控件之单图片实现Button的点击效果(雷惊风)
- Android中ImageButton自定义按钮的按下效果的代码实现方法,附网上2种经典解决方法。 .
- Android中ImageButton自定义按钮的按下效果的代码实现方法,附网上2种经典解决方法
- 转载 :Android中ImageButton自定义按钮的按下效果的代码实现方法,附网上2种经典解决方法。
- Android中ImageButton自定义按钮的按下效果的代码实现方法,附网上2种经典解决方法
- Android中ImageButton自定义按钮的按下效果的代码实现方法,附网上2种经典解决方法。
- iOS自定义button抖动效果并实现右上角删除按钮
- Extjs 利用panel html属性加入DIV实现如TextArea的效果,并且能正确使用html标签,实现如向textarea文本域嵌入按钮button或其他控件,,而且兼容firefox
- 【原创】Android中ImageButton自定义按钮的按下效果的代码实现方法,附网上2种经典解决方法。
- Android中ImageButton自定义按钮的按下效果的代码实现方法
- Android ImageButton自定义按钮的按下效果的代码实现方法分享
- ImageButton自定义按钮的按下效果的高效实现方法(非一般)
- Android进阶——自定义View之继承系统控件实现自带删除按钮动画效果和软键盘自动悬浮于文本框下方
- ios cocos2d实现progress(进度条)效果控件源码
- Android中ImageButton自定义按钮的按下效果的代码实现方法
- Android中ImageButton自定义按钮的按下效果的代码实现方法,附网上2种经典解决方法。
- Android中ImageButton自定义按钮的按下效果的代码实现方法,附网上2种经典解决方法。