【问题标题】:Determine if image being dragged is inside a specific area确定被拖动的图像是否在特定区域内
【发布时间】:2017-02-14 06:32:13
【问题描述】:

我有一个游戏,我正在将顶层上的方形块移动到下面的圆形上方,这些圆形是不可移动的。因此,当块的拖动停止时,我想运行检查或 if 语句以查看我正在移动的块 (myBlocks[objectDragging]) 是否在我的圆中心的 x 像素量内 (myCircles[objectDragging] ])。 objectDragging 只是获取点击图像的标签。可匹配的圆圈将具有相同的标签。一切正常,我只是不知道如何检查我要放置的块(它的中心点)是否在圆中心点的这么多像素内。

我正在使用的一些东西:

var myBlocks = [UIImageView]()
var myCircles = [UIImageView]()

let objectDragging = recognizer.view?.tag

if myBlocks[objectDragging!].center.x == myCircles[objectDragging!].center.x {
        ...
        } //this checks for an exact match of center.x where-as I want to check
//if the center.x for myBlocks[objectDragging!] is <= we'll say,
//25, pixels of the myCircles[objectDragging!].center.x

【问题讨论】:

    标签: ios swift uiimageview swift3


    【解决方案1】:

    在这里讨论找到两个CGPoints之间的距离:

    How to find the distance between two CG points?

    根据卢修斯(答案 2)

    您可以使用 hypot() 或 hypotf() 函数来计算 斜边。给定两个点 p1 和 p2:

    CGFloat distance = hypotf(p1.x - p2.x, p1.y - p2.y);
    

    在您的 myBlocks.center 和 myCircles.center 中为 p1 和 p2 订阅,然后

    if distance < 25 {
        ...
        }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-06-28
      • 2011-07-30
      • 2018-10-06
      • 2014-11-08
      • 2016-01-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多