已知宽高,在某区域获取随机的中心center,或者区域 rect

2018-08-06  本文已影响9人  AZander

// 获取随机中心位置,在某个视图不超出某个区域的情况下

    static func randomCenterWith(in rect:CGRect, w:CGFloat, h:CGFloat) -> CGPoint {
        let sW = rect.size.width
        let sH = rect.size.height
        let minX = w/2
        let maxX = sW - w/2
        let randomX = CGFloat(arc4random_uniform(UInt32(maxX - minX))) + minX
        let minY = h/2
        let maxY = sH - h/2
        let randomY = CGFloat(arc4random_uniform(UInt32(maxY - minY))) + minY
        let randomCenter = CGPoint(x: randomX, y: randomY)
        return randomCenter
    }
上一篇 下一篇

猜你喜欢

热点阅读