我並不真正熟悉objC,並尋找一種解決方案來構建可用「步驟」滑動的滑塊。在移動滑塊時,數值會增加,如數值+ = 10或數值+ = 100。我怎麼做?具有constum步驟的iphone滑塊
1
A
回答
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;
}
它調用valueForLocation和locationFromValu Ë方法:
- (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
相關問題
- 1. 使滑塊步驟具體值
- 2. Jquery UI滑塊步驟0.5
- 3. 具有非線性/指數/對數步驟的JQuery UI滑塊
- 4. jQuery UI中的不同步驟滑塊
- 5. jQuery UI滑塊自定義步驟
- 6. jQuery用戶界面滑塊步驟
- 7. Javascript/HTML 5滑塊步驟問題
- 8. 5個步驟順利jquery Ui滑塊
- 9. ASP.NET AJAX滑塊跳過步驟
- 10. TensorBoard更改圖片滑塊步驟
- 11. 輸入範圍滑塊步驟
- 12. 如何根據滑塊值動態設置jquerymobile滑塊的步驟?
- 13. 滑動滑塊同步滑塊 - 3
- 14. jQuery Mobile的滑塊時的步驟數量很大行爲不
- 15. 具有步驟的數學求解器
- 16. 的UIButton與具有多於1步驟
- 17. 創建具有不同的步驟
- 18. 交錯上/下步驟的jQuery UI範圍滑塊
- 19. 精確計算範圍滑塊的步驟
- 20. 將滑塊中的步驟鏈接到圖像
- 21. 逗號分隔的步驟,具有相同的值的多個步驟
- 22. 對於具有一個步驟,golang
- 23. 是否可以使用滑塊步驟表單Element Without Slider?
- 24. jQuery步驟滑塊輸出多個變量(價格)
- 25. jQuery Mobile滑塊 - 使輸入能夠違反步驟
- 26. 如何創建自定義步驟滑塊/範圍組?
- 27. 如何用4個步驟製作範圍滑塊?
- 28. 如何向角度材質滑塊添加步驟
- 29. 滑塊一步WP7
- 30. 有助於解鎖iPhone的滑塊