2011-03-28 50 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