2011-03-28 51 views
1

我并不真正熟悉objC,并寻找一种解决方案来构建可用“步骤”滑动的滑块。在移动滑块时,数值会增加,如数值+ = 10或数值+ = 100。我怎么做?具有constum步骤的iphone滑块

回答

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:我添加如果为例步骤设置为0,滑块可以在照常上班(self.steps!= 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; 

} 

它调用valueForLocationlocationFromValu Ë方法:

- (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]; 

希望它可以是有用的

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