iphone滑块与自定义步骤

时间:2011-03-28 18:15:33

标签: iphone slider uislider

我对objC并不熟悉并且正在寻找一种解决方案来构建一个滑动的“滑动”步骤。在移动滑块时,值增加,如值+ = 10或可能值+ = 100。我该怎么做?

2 个答案:

答案 0 :(得分:1)

我用这种方式创建了我的离散滑块:

#import <UIKit/UIKit.h>


@interface DiscreteSlider : UISlider {
    int step;
}

@property (nonatomic) int step;

@end

该实施:

#import "DiscreteSlider.h"


@implementation DiscreteSlider

@synthesize step;

- (void) recalcuateStep
{
    float lValue = self.value;
    int lLowerValue = (int) ( lValue / self.step );
    float lDifference = lValue - ( lLowerValue * step );
    float lHalfStep = ((float) step) / 2;
    if( lDifference < lHalfStep ) {
    } else {
        self.value = (float) ( lLowerValue + step );
        [self sendActionsForControlEvents:UIControlEventValueChanged];
    }
}

- (void) touchesMoved:(NSSet *) touches
            withEvent:(UIEvent *) event
{
    [super touchesMoved:touches withEvent:event];
    NSLog( @"DS.touchesMoved(), event: %@", event );
    [self recalcuateStep];
}

- (void) endTrackingWithTouch:(UITouch *) touch
                    withEvent:(UIEvent *) event
{
    [super endTrackingWithTouch:touch withEvent:event];
    NSLog( @"DS.endTrackingWithTouch(), event: %@", event );
    [self recalcuateStep];
}

- (int) step {
    return ( step ? step : 1 );
}

@end

答案 1 :(得分:1)

我修改了上面的代码以获得自定义的Cocos2D滑块http://yannickloriot.com/library/ios/cccontrolextension/Classes/CCControlSlider.html

它有属性

@property (nonatomic, readwrite) float value;
@property (nonatomic, readwrite) minimumValue;
@property (nonatomic, readwrite) maximumValue;
@property (nonatomic, readwrite) int steps;

重新计算

- (void)recalcuateValue
{
    float stepValues[self.steps];
    stepValues[0] = self.minimumValue;
    stepValues[self.steps - 1] = self.maximumValue;
    for(int i = 1; i < self.steps; i++){
        stepValues[i] = i * (self.maximumValue - self.minimumValue) / (self.steps - 1);
        if (self.value < stepValues[i] && self.value > stepValues[i-1]){
            self.value = (self.value > (stepValues[i] - stepValues[i-1]) / 2 + stepValues[i-1])?stepValues[i]:stepValues[i-1];
        }
    }  

}

ccTouchesEnded:我添加if(self.steps!= 0),对于案例步骤设置为0 ,滑块可以在普通模式下工作 < / p>

- (void)ccTouchEnded:(UITouch *)touch withEvent:(UIEvent *)event
{
     if ([self isSelected]){
        self.value          = [self valueForLocation:_thumbSprite.position];
        if(self.steps != 0) {
            [self recalcuateValue];
            [_thumbSprite setPosition:[self locationFromValue:self.value]];
        } 
    }
    self.thumbSprite.color  = ccWHITE;
    self.selected           = NO;

}

它调用 valueForLocation locationFromValue 方法:

- (float)valueForLocation:(CGPoint)location
{
    float percent           = location.x / _backgroundSprite.contentSize.width;
    return _minimumValue + percent * (_maximumValue - _minimumValue);
}

- (CGPoint)locationFromValue:(float)value{
    float percent = self.value / self.maximumValue;
    return ccp(percent * _backgroundSprite.contentSize.width, _backgroundSprite.position.y);
}

一个使用示例。我需要一个有3个步骤的滑块,每个步骤的值为0,1和2:

    self.Slider = [CCControlSlider sliderWithBackgroundFile:@"sliderTrack.png"
                                                      progressFile:@"sliderProgress.png"
                                                         thumbFile:@"sliderThumb-hd.png"]; progressFile:@"sliderProgress.png" thumbFile:@"sliderThumb-hd.png"];
    self.Slider.minimumValue = 0.0f;
    self.Slider.maximumValue = 2.0f;
    self.Slider.steps = 3;
    self.Slider.value = [[GameSettings sharedSettings] defaultAILevel];
    [self.Slider addTarget:self action:@selector(onSliderValueChanged:) forControlEvents:CCControlEventValueChanged];

希望它可能有用