How to invert a CGFloat in Swift 3?

533 views Asked by At

I have a CGFloat a and need a CGFloat b, inverted from a.

If a is 0, I need b to be 100. If a is 1, I need b to be 99 ans so on.

The only solution I came up with right now is a function like the following:

func turn(_ a: CGFloat) -> CGFloat {
    switch a {
    case 0:
        return 100
    case 1:
        return 99
    case 2:
        return 98
    case 3:
        return 97
    case 4:
        return 96
    case 5:
        return 95
    case 6:
        return 94
    case 7:
        return 93
    case 8:
        return 92
    case 9:
        return 91
    case 10:
        return 90

But a) is that insane to write for 100 Floats, and b), I need it for an animation and I receive a in UITableView.offset Floats, so the steps are with x decimal places and therefore I want b to be in decimal steps as well (to have the animation smooth).

Any ideas? Help is very appreciated.

1

There are 1 answers

1
Nirav D On BEST ANSWER

It's simply like this.

let invert: CGFloat = 100
let a: CGFloat = 0
let b = invert - a

Or Simply

let b = 100 - a