不要让UIImageViews与随机的CGPoint相交
创始人
2025-01-10 21:00:32
0

要解决不让UIImageViews与随机的CGPoint相交的问题,可以使用以下方法:

  1. 确定UIImageView的位置和大小:首先,确定每个UIImageView的frame或bounds,以便计算出它们的位置和大小。

  2. 生成随机CGPoint:使用arc4random_uniform函数生成一个随机的CGPoint。确保CGPoint的范围不与任何UIImageView的frame相交。

  3. 检查相交:使用CGRectIntersectsRect函数检查生成的随机CGPoint是否与任何其他UIImageView的frame相交。如果相交,则返回第2步并重新生成随机CGPoint。

下面是一个示例代码,演示了如何实现上述解决方法:

func generateRandomCGPoint(forImageViews imageViews: [UIImageView], inView containerView: UIView) -> CGPoint {
    var randomPoint: CGPoint
    
    repeat {
        let minX = containerView.bounds.origin.x
        let minY = containerView.bounds.origin.y
        let maxX = containerView.bounds.size.width
        let maxY = containerView.bounds.size.height
        
        randomPoint = CGPoint(x: minX + CGFloat(arc4random_uniform(UInt32(maxX))), y: minY + CGFloat(arc4random_uniform(UInt32(maxY))))
        
    } while intersects(withImageViews: imageViews, atPoint: randomPoint)
    
    return randomPoint
}

func intersects(withImageViews imageViews: [UIImageView], atPoint point: CGPoint) -> Bool {
    for imageView in imageViews {
        if imageView.frame.contains(point) {
            return true
        }
    }
    return false
}

// 使用示例
let imageView1 = UIImageView(frame: CGRect(x: 0, y: 0, width: 100, height: 100))
let imageView2 = UIImageView(frame: CGRect(x: 200, y: 200, width: 100, height: 100))
let containerView = UIView(frame: CGRect(x: 0, y: 0, width: 400, height: 400))

let imageViews = [imageView1, imageView2]
containerView.addSubview(imageView1)
containerView.addSubview(imageView2)

let randomPoint = generateRandomCGPoint(forImageViews: imageViews, inView: containerView)
print(randomPoint)

在上面的示例中,我们首先创建了两个UIImageView和一个包含它们的容器UIView。然后,我们使用generateRandomCGPoint函数生成一个不与UIImageView相交的随机CGPoint,并将其打印出来。generateRandomCGPoint函数会循环生成随机CGPoint,直到找到一个不与任何UIImageView相交的点为止。

相关内容

热门资讯

黑科技科技(wepoke智能a... 黑科技科技(wepoke智能ai)wepower使用说明书(wepOkE)素来是真的有挂(确实有挂)...
黑科技模拟器(wepoke智能... WePoker透视辅助工具核心要点解析‌,黑科技模拟器(wepoke智能ai)wepoke是机器发牌...
黑科技脚本(wepokeai代... 您好,这款游戏可以开挂的,确实是有挂的,需要了解加微【136704302】很多玩家在这款游戏中打牌都...
黑科技辅助挂(wepoke真的... 黑科技辅助挂(wepoke真的有挂)wepoke 软件(wEPOKE)固有真的有挂(有挂猫腻)-哔哩...
黑科技辅助挂(wepoke黑科... 黑科技辅助挂(wepoke黑科技)wepoke软件透明挂演示(wEpOke)一直真的有挂(真的有挂)...
黑科技app(wepoke真的... 此外,数据分析德州()辅助神器app还具备辅助透视行为开挂功能,通过对客户透明挂的深入研究,你可以了...
黑科技智能ai(wepoke辅... 黑科技智能ai(wepoke辅助插件)wepoke软件透明(WEPOke)先前是有挂(果真有挂)-哔...
黑科技模拟器(wepoke透明... 黑科技模拟器(wepoke透明黑科技)wepoke德州扑克用ai代打(WEPOKE)最初存在有挂(有...
黑科技插件(wepoke辅助插... 黑科技插件(wepoke辅助插件)wepoke软件透明(WEPOke)从来是真的有挂(有挂解惑)-哔...
黑科技美元局(wepoke智能... 黑科技美元局(wepoke智能ai)wepoke辅助真的假的(wepoKE)原先真的有挂(竟然有挂)...