不正確INT值的自定義UISlider值的效果我有一個UIView
其中alpha
是from 0.8 to 0.0
它代表100 - 0
(整數值)請與目標C
當我向左滑動屏幕或有權作出增加或降低該值。但我總是錯過一些值與4,如14,24等。
我知道這可能是準確性問題。但不知道如何在短時間內解決這個問題。謝謝!
這裏是我的代碼:
- (void) panFromLeftOrRight:(UIPanGestureRecognizer *)gestureRecognizer {
//NSLog(@"%f",_valueView.alpha);
if (_valueView.alpha > 0.8f) {
_valueView.alpha = 0.8;
return;
}
if (_valueView.alpha < 0.0) {
_valueView.alpha = 0.0;
return;
}
CGPoint point = [gestureRecognizer translationInView:_valueView];
if ((point.x - _lastX) > 10) {
_valueView.alpha -= 0.01;
_lastX = point.x;
}
if ((point.x - _lastX) < -10) {
_valueView.alpha += 0.01;
_lastX = point.x;
}
if(gestureRecognizer.state == UIGestureRecognizerStateEnded){
_lastX = 0.0;
}
float db = _valueView.alpha * 125.0f;
NSLog(@"%f",db);
NSLog(@"(%0.000f)",floor(db));
_value.text = [NSString stringWithFormat:@"%0.0f",db];
//OR
_value.text = [NSString stringWithFormat:@"%d",(int)db];
}
這裏是db
結果:
2015-09-28 18:21:41.121 Rols[70691:2929217] 100.000000
2015-09-28 18:21:41.847 Rols[70691:2929217] 98.750000
2015-09-28 18:21:41.873 Rols[70691:2929217] 97.500008
2015-09-28 18:21:42.225 Rols[70691:2929217] 96.250008
2015-09-28 18:21:42.854 Rols[70691:2929217] 95.000008
2015-09-28 18:21:42.874 Rols[70691:2929217] 93.750008
2015-09-28 18:21:43.836 Rols[70691:2929217] 92.500008
2015-09-28 18:21:44.716 Rols[70691:2929217] 91.250008
所以對於97.5
,如果使用
// for nearest integer (3.4999 -> 3, 3.5 -> 4):
int result = (int)roundf(myFloat);
我失去了97
。
根本沒有94
。
請不要關閉這一點,這是不一樣的https://stackoverflow.com/a/8786962/291240
?你爲什麼乘以'125.0'?你爲什麼要轉換爲'int'呢? – Droppy
這個問題使用這個概念的側面菜單 –
是的,就像UISlider,但值應該是countinuous整數 –