iOS cocos2d实现slider(滑块)控件效果源码

10 篇文章 0 订阅
9 篇文章 1 订阅


开发人员: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



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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值