LoginSignup
8

More than 5 years have passed since last update.

[Swift] 画面内のランダムな座標を返すメソッドを作った

Last updated at Posted at 2015-08-13

コード

シンプルですが、備忘録も兼ねて

Swift2.0
// 通常
func randPoint() -> CGPoint{
    let randWidth  = CGFloat(arc4random_uniform(UInt32(self.frame.width)))
    let randHeight = CGFloat(arc4random_uniform(UInt32(self.frame.height)))
    let randPoint  = CGPoint(x:randWidth,y:randHeight)

    return randPoint 
}

// 2行で書きたい場合
func randPoint() -> CGPoint{
    let randPoint = CGPoint(x:CGFloat(arc4random_uniform(UInt32(self.frame.width))),y:CGFloat(arc4random_uniform(UInt32(self.frame.height))))
    return randPoint
}

使い方

Swift2.0
var testPoint:CGPoint = randPoint()
print(testPoint)
//=> e.g. (195.0, 42.0)

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
8