【发布时间】:2014-10-14 19:44:47
【问题描述】:
我想在一个圆圈内移动一个 UIView。 UIView 移动圆内的每个点,但不触及圆的边界线。我正在计算圆圈和 UIView 之间的距离。
var distance = sqrt(
pow((touchPoint.x - selfCenter.x), 2) + pow((touchPoint.y - selfCenter.y), 2)
)
并限制 UIView 向圈外移动
if distance <= radius {
theUIView.center = touchPoint
}
问题从这里开始,如果触摸从圆圈中移出,则 UIView 会卡在圆圈内的边框上。这就是为什么我尝试编写 else 语句的原因。
if distance <= radius {
theUIView.center = touchPoint
} else {
theUIView.center = CGPointMake(
touchPoint.x / distance * radius,
touchPoint.y / distance * radius
)
}
问题是,如果触摸继续移动,我如何将 UIView 保持在圆圈内并继续移动。一个提示会很棒。
这里有类似的问题 -like this- 但没有帮助。
【问题讨论】:
标签: uiview swift core-animation