Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/110.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/19.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 动态获取CGRect坐标_Ios_Swift_Coordinates_Cgrect - Fatal编程技术网

Ios 动态获取CGRect坐标

Ios 动态获取CGRect坐标,ios,swift,coordinates,cgrect,Ios,Swift,Coordinates,Cgrect,我试图在屏幕上获得两个UIImageView之间的距离,为此我计划从图像1(不可移动)中减去图像2(可移动)的中心坐标。 为此,我需要能够动态地获得这些图像的坐标,我使用的是这样的东西: var centerBoardX = CGRectGetMidX(BlackBoard.frame) var centerBoardY = CGRectGetMidY(BlackBoard.frame) var centerRoundX = CGRectGetMidX(round1.fr

我试图在屏幕上获得两个
UIImageView
之间的距离,为此我计划从图像1(不可移动)中减去图像2(可移动)的中心坐标。 为此,我需要能够动态地获得这些图像的坐标,我使用的是这样的东西:

    var centerBoardX = CGRectGetMidX(BlackBoard.frame)
    var centerBoardY = CGRectGetMidY(BlackBoard.frame)
    var centerRoundX = CGRectGetMidX(round1.frame)
    var centerRoundY = CGRectGetMidY(round1.frame)

    println(centerBoardX - centerRoundX)
    println(centerBoardY - centerRoundY)
但是,即使我在整个屏幕上移动image2,基于两个UIImageView的初始坐标,我总是得到相同的结果

怎么了?为什么我只能得到初始坐标而不能得到新坐标?
谢谢

好的,所以我发现了不正确的地方。 我的UIImageView是由帧定义的,我得到的是帧的坐标,它们总是相同的。 为了获得正确的坐标,我需要从对象的superview获取信息。 所以工作代码是

var centerBoardX = BlackBoard.superview?.center.x
var centerBoardY = BlackBoard.superview?.center.y
var centerRoundX = Round1.superview?.center.x
var centerRoundY = Round1.superview?.center.y

println(centerBoardX! - centerRoundX!)
println(centerBoardY! - centerRoundY!)

现在它工作得很好

使用调试器查看正在发生的情况,在移动视图时观察帧。日志记录也有帮助,也可以考虑在运行应用程序时查看3D视图中的所有视图,没有什么可以添加到应用程序中去使用它。可能您没有移动您认为是的视图。可能检查
bounds
而不是
frame
或检查
round1.center
?也许可以检查
位置?这完全取决于您如何移动UIImageView…您如何移动图像视图?通过滚动视图?通过转变?你自己换镜框?