Find X-value position of a label inside a view in Swift
Asked Answered
V

1

6

I want to calculate the distance between x-value of two squares positioned in a view.

I did not work because in the frame there is also the y-value and do not know how to select only one. how can I add the x-values from the position of the squares?

var redSquare: UIView = UIView()
var blueSquare: UIView = UIView()
var difference : Double = 0.0

func calc() {
difference = redSquare.frame - blueSquare.frame  
}
Vue answered 5/12, 2014 at 13:32 Comment(0)
L
17
difference = Double(redSquare.frame.origin.x - blueSquare.frame.origin.x)
Lacee answered 5/12, 2014 at 13:58 Comment(1)
You should really use CGRectGetMinX(redSquare.frame). CGRects can actually have negative width and height so the top left of it may not always be its origin.Whin

© 2022 - 2024 — McMap. All rights reserved.