Search code examples
iosswiftcgrect

Reversing CGRect - Swift


How can I reverse my origin.x? my origin.x started from right I want start from left. For example these origins of x in my application started from the far right:

1) 351.0

2) 304.0

3) 257.0

4) 210.0

5) 163.0

6) 116.0

7) 69.0

8) 22.0

How can I reverse these origins and started from 22.0 and end to 351.0 ?

Code:

var ansWithoutSpaces = String()
ansWithoutSpaces = answersString.replacingOccurrences(of: " ", with: "")

var targetHeigt = CGFloat()
let viewWidth = self.view.frame.size.width  

if viewWidth == 320 {        
    targetHeigt = 2.5
} else {            
    targetHeigt = 5
}

let yAxis : Int = Int((self.view.frame.height) * 60%)     
let lenghtOfChar = ansWithoutSpaces as NSString
// char count
let width: Int = Int(view.frame.size.width) - 40
// frame width
var targetWidth: Int = (width - (lenghtOfChar.length - 1) * 5) / lenghtOfChar.length

if targetWidth > 50 {
    if viewWidth == 320 {
        targetWidth = 38 
    } else if viewWidth == 375 {
        targetWidth = 45
    } else {
        targetWidth = 50
    }
}

let totalWidth: Int = (targetWidth * lenghtOfChar.length) + ((lenghtOfChar.length - 1) * 5)

for x in 0..<ansWithoutSpaces.count {
    let xAxis: Int = (width / 2) - (totalWidth / 2) + (x * targetWidth) + (x * 5) + 20
    let targetLabel = UILabel(frame: CGRect(x: CGFloat(xAxis), y: CGFloat(yAxis), width: CGFloat(targetWidth), height: targetHeigt))

        ...    
}

Solution

  • @Nordeast solved it:

    for x in (0..<ansWithoutSpaces.count).reversed() {
            ...    
    }