5

I've wrote this code down here but it's not working the way I want, I have only got the random position in the all view, my meaning is to get random positions on the edges of the screen. How can I do it?

    let viewHeight = self.view!.bounds.height
    let viewWidth = self.view!.bounds.width

    randomYPosition = CGFloat(arc4random_uniform(UInt32(viewHeight)))
    randomXPosition = CGFloat(arc4random_uniform(UInt32(viewWidth)))
Eric Aya
  • 69,473
  • 35
  • 181
  • 253
David Dume
  • 65
  • 1
  • 1
  • 8

4 Answers4

9

Use the following property:

For Max:

view.frame.maxX
view.frame.maxY

For minimum:

view.frame.minX
view.frame.minY
Ramesh_T
  • 1,251
  • 8
  • 19
2

If you really want the bounds of the screen, then you should see How to get the screen width and height in iOS?, which tells you that you need to look at the UIScreen object for its dimensions. In Swift, the code works out to:

let screenRect = UIScreen.mainScreen().bounds
let screenWidth = screenRect.size.width
let screenHeight = screenRect.size.height

That really gives you the screen width and height, but it'd be strange if the screen origin weren't at (0,0). If you want to be sure, you can add the origin's coordinates:

let screenWidth = screenRect.size.width + screenRect.origin.x
let screenHeight = screenRect.size.height + screenRect.origin.y

All that said, there's usually little reason to look at the screen itself. With iOS now supporting split screen, your app may only be using part of the screen. It'd make more sense to look at the app's window, or even just at the view controller's view. Since these are both UIViews, they both have a bounds property just like any other view.

Community
  • 1
  • 1
Caleb
  • 124,013
  • 19
  • 183
  • 272
0

This should give you a random number in the range 0 - ViewHeight and 0 - ViewWidth respectively

CGFloat(arc4random_uniform(UInt32(0..viewHeight)))

CGFloat(arc4random_uniform(UInt32(0..viewWidth)))

Now you need to construct the position based on the 4 cases

  1. X = 0, Y = Random (0 - ViewHeight) --> Left side
  2. X = self.view!.bounds.width, Y = Random (0 - ViewHeight) --> Right side
  3. X = Random (0 - ViewWidth), Y = 0 --> Bottom side
  4. X = Random (0 - ViewWidth), Y = self.view!.bounds.width --> Top side
D. Mihai
  • 41
  • 5
0

If I got you correctly, what you want is random points which fall in the edges of the view. That means on the four sides of the view, either x or y is fixed. So here is the solution :

  1. top wall random points( where y is fixed to 0, and varying x):

topRandomPoint = CGPointMake(CGFloat(arc4random_uniform(UInt32(viewWidth))), 0)

  1. right side wall points ( where x is fixed to max-width (e.g 320), and varying y)

rightRandomPoint = CGPointMake(viewWidth, CGFloat(arc4random_uniform(UInt32(viewHeight))))

  1. Bottom random points ( where y is fixed to max-height (e.g 568) and x varying)

bottomRandomPoint = CGPointMake(CGFloat(arc4random_uniform(UInt32(viewWidth))), viewHeight)

  1. Left random points (where x is fixed to 0 and y is varying)

leftRandomPoint = CGPointMake(0, CGFloat(arc4random_uniform(UInt32(viewHeight))))

Tushar
  • 3,022
  • 2
  • 26
  • 26