MSCircularSlider icon indicating copy to clipboard operation
MSCircularSlider copied to clipboard

Setting CurrentValue programmatically is not updating UI when minimum value is more than 0.

Open LReema opened this issue 2 years ago • 3 comments

Descrepencies when trying to set current Values of the slider. I have set following values: maxRotationAngle: 270, minimumValue: 50.0, maximumValue: 90.0, currentValue = 62, programmatically. When I query the currentValue it returns me 21.8.... and when I query the angle it gives 147. My calculations are: when starting from Minimum, when reaching 70(middle of max and min) currentValue the angle should be 270/2= 135. So, for 62 angle should be less than 135. Could you please correct me if I am missing something or provide a fix if this is not working fine? This is urgent request. The slider works great, for changing the slider value on UI.

LReema avatar Mar 25 '23 02:03 LReema

Hi,

I haven't had the time to maintain this framework, unfortunately. I did have a look for you nonetheless, and it does seem to be a bug.

Basically, the currentValue and angle setters use two internal conversion functions valueFrom(angle) and angleFrom(value). I seem to have not accounted for the minimumValue being non-zero.

Since you urgently need this fixed, change the functions (at the bottom of the MSCircularSlider.swift file) to the following and it should work as expected:

    /** Calculates the angle from north given a value */
    internal func angleFrom(value: Double) -> CGFloat {
        // added "- minimumValue" to the numerator
        return (CGFloat(value - minimumValue) * maximumAngle) / CGFloat(maximumValue - minimumValue)
    }

    /** Calculates the value given an angle from north */
    internal func valueFrom(angle: CGFloat) -> Double {
        // added "+ minimumValue"
        return (maximumValue - minimumValue) * Double(angle) / Double(maximumAngle) + minimumValue
    }

I will try to update the pod and the repo accordingly within the week, just need to make sure this actually works for other edge-cases.

If you have the time to apply this and test it properly, please fork the repo and submit a PR, you'll be added as a contributor.

ThunderStruct avatar Mar 25 '23 03:03 ThunderStruct

Thank you for the quick response. The fix works fine when minimum value is set.

LReema avatar Mar 27 '23 04:03 LReema

I am unable to create a new PR as code is same. The changes are fine for all the conditions. I tried with different maximum angles - 90,180,270,360 and different minimum and maximum values. Changing the values in UI and programmatically to the slider looks fine.

LReema avatar Mar 27 '23 04:03 LReema