- 中心
// 获取随机中心位置,在某个视图不超出某个区域的情况下
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
}
-
区域
static func randomRectWiht(in rect:CGRect, w:CGFloat, h:CGFloat) -> CGRect { let randomCenter = self.randomCenterWith(in:rect, w: w, h: h) let rect = CGRect(x: randomCenter.x + w/2, y: randomCenter.y + h/2, width: w, height: h) return rect }
网友评论