Я изменил приведенный выше код для пользовательского слайдера 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: я добавил, если (self.steps! = 0), для шагов установлено значение 0 , ползунок может работать в обычном режиме
- (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];
Надеюсь, это может быть полезно