For positioning an element random in the screen but within the boundarys I need the resolution/screen size in swift. I already fount out how to get it with CGGraphics. Unfortunately I have to calculate a bit and need it as type UInt32
, not CGFloat
. I cannot use casting that way. Any ideas?
let screenSize: CGRect = UIScreen.mainScreen().bounds
let screenWidth = screenSize.width
//What I want to do
let screenHeight = screenSize.height-arc4random_uniform(screenSize.height)
If you want random X and Y positions for your element you can do something like this:
let screenSize = UIScreen.main.bounds
let randomXPos = CGFloat(arc4random_uniform(UInt32(screenSize.width)))
let randomYPos = CGFloat(arc4random_uniform(UInt32(screenSize.height)))
let screenSize = UIScreen.main.bounds
let randomXPos = CGFloat.random(in: 0..<screenSize.width)
let randomYPos = CGFloat.random(in: 0..<screenSize.height)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With