I'm trying to use one variable (diameter) to act in concert with another variable (baseCurve), such that for every 0.5 increase to diameter, baseCurve is adjusted down by 0.25, and for every 0.5 decrease to diameter, baseCurve is adjusted up by 0.25.
The diameter is adjusted in 0.1 increments, by clicking an "increase" or "decrease" button. Seems simple enough, but I can't get around the problem that arises when the user adds less than the 0.5, and then reverses course.
Example (code works as intended): starting diameter = 9.0; starting baseCurve = 42.50
User clicks "Increase" 5x, now diameter = 9.5; baseCurve = 42.25
Example of problem: starting diameter = 9.0; starting baseCurve = 42.50
User clicks "Increase" 1x, now diameter = 9.1; baseCurve = 42.50
User then clicks "Decrease" 1x, now diameter = 9.0; baseCurve = 42.75.
The problem is, no adjustment should have been made, but my code is set up to make adjustments based on a modulus result, yielding the problem.
Can anyone point me to an approach that will basically allow the user to adjust baseCurve by 0.25 for every net change of 0.5 in diameter? The approach that I've taken, using a counter and modulus check isn't working, at least as I've set it up. I've also tried a switch, but can't avoid the same problem. Is there some way to use a couple of booleans to make this work? Many thanks in advance.
var adjustmentSum = 0
var baseCurve = 45.00
var diameter = 9.0
let diameterAdjustmentValue : Float = 0.1
let baseCurveAdjustmentValue : Float = 0.25
@IBAction func increaseDiameter(sender: AnyObject) {
adjustmentSum++
diameter = diameter + diameterAdjustmentValue
if adjustmentSum % 5 == 0 {
baseCurve = baseCurve - baseCurveAdjustmentValue
}
}
@IBAction func decreaseDiameter(sender: AnyObject) {
adjustmentSum--
diameter = diameter - diameterAdjustmentValue
if adjustmentSum % 5 == 0 {
baseCurve = baseCurve + baseCurveAdjustmentValue
}
}
答案 0 :(得分:1)
如果我理解你的问题,你甚至不需要使用模数运算符。您可以只保留增量/减量的常规计数器,并在每次更改Internet connection
baseCurve