开发人员:Jason's.Alex QQ:531401335
csdn博客:http://blog.csdn.net/RuShrooM
//
// CCSlider.h
// DiceGameBox2D
//
// Created by jasonsalex on 13-1-29.
//
//滑动按钮
//开发人员:Jason's.Alex
//QQ:531401335
#import "CCSprite.h"
#import "cocos2d.h"
@interface CCSlider : CCSprite<CCTargetedTouchDelegate>
{
id target;//调用对象
SEL callBack; //调用函数
float maxValue;//最大数值
float minValue;//最小数值
float curValue;//当前数值
CCSprite *slider;//划块按钮
CGPoint curMove;
}
@property(readwrite)float maxValue;
@property(readwrite)float minValue;
@property(readonly)float curValue;
+(id)sliderWithCCSlider:(id)button background:(id)bg target:(id)target callback:(SEL)cb;
-(id)sliderWithInit:(id)button background:(id)bg target:(id)target callback:(SEL)cb;
-(void)setCurValue:(float)curValue;//修改当前数值
@end
//
// CCSlider.m
// DiceGameBox2D
//
// Created by jasonsalex on 13-1-29.
//
//
//开发人员:Jason's.Alex
//QQ:531401335
#import "CCSlider.h"
@implementation CCSlider
@synthesize maxValue;
@synthesize minValue;
@synthesize curValue;
+(id)sliderWithCCSlider:(id)button background:(id)bg target:(id)target callback:(SEL)cb
{
return [[[self alloc] sliderWithInit:button background:bg target:target callback:cb]autorelease];
}
-(id)sliderWithInit:(id)button background:(id)bg target:(id)tar callback:(SEL)cb
{
if(self=[super init])
{
target=tar;
callBack=cb;
curValue=0;
maxValue=0;
minValue=0;
CCSprite* bgSprite=[CCSprite spriteWithFile:bg];
slider=[CCSprite spriteWithFile:button];
[slider setPosition:ccp(0,bgSprite.boundingBox.size.height*0.5f)];
[self setDisplayFrame:[bgSprite displayedFrame]];
[self addChild:slider z:10];
curMove=[slider position];
}
return self;
}
-(void)onEnter
{
[[CCTouchDispatcher sharedDispatcher] addTargetedDelegate:self priority:-128 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];
touchLocal=[self convertToNodeSpace:touchLocal]; //转换为当前节点的坐标
return CGRectContainsPoint([slider boundingBox], touchLocal);
}
-(void)ccTouchMoved:(UITouch *)touch withEvent:(UIEvent *)event
{
CGPoint touchMove=[touch locationInView:[touch view]];
touchMove=[[CCDirector sharedDirector] convertToGL:touchMove];
touchMove=[self convertToNodeSpace:touchMove];
CGPoint subMove=ccpSub(touchMove, curMove);
CGPoint moveTo=ccp(slider.position.x+subMove.x,curMove.y);
moveTo=[self convertToWorldSpace:moveTo];
if(CGRectContainsPoint([self boundingBox], moveTo)) //判断是否超出了范围
{
[slider setPosition:[self convertToNodeSpace:moveTo]];
CGSize size=[self boundingBox].size;
float def=maxValue-minValue;
curValue=slider.position.x*(def/size.width); //将当前滑块坐标转换为数值
[target performSelector:callBack withObject:self];
}
curMove=[slider position];
}
-(void)ccTouchEnded:(UITouch *)touch withEvent:(UIEvent *)event
{
}
-(void)setCurValue:(float)value //修改当前数值
{
if(value<=maxValue&&value>=minValue)
{
CGSize size=[self boundingBox].size;
float def=maxValue-minValue;
float x=(value/def)*size.width; //将数值转换为坐标点
[slider setPosition:ccp(x, slider.position.y)];
curValue=value;
}
}
@end