ios - UIPanGestureRecognizer - 仅在连接 2 个点的线路径内拖动 UIView
问题描述
我有一条线 ( CAShapeLayer
) 连接两点。这两个点可以在屏幕上的任何位置,并且它们的坐标是已知的。中间的点也有 aUIPanGestureRecognizer
但我希望它只能在线条内拖动(所以它不能离开线条 - 线条必须随时直接穿过中间点的中心)。
我知道我可以计算点的可拖动位置
let translation = gesture.translation(in: self.view)
并通过 X 或 Y 轴限制它,但是这里需要什么样的计算才能将它只拖到行内?
解决方案
谢谢您的回答!我设法找到了最合适的解决方案。我使用这个答案的公式: https ://stackoverflow.com/a/57383437/1088286
function getPositionAlongTheLine(x1, y1, x2, y2, percentage) {
return {x : x1 * (1.0 - percentage) + x2 * percentage, y : y1 * (1.0 - percentage) + y2 * percentage};
}
这就是我在didMove:
UIPanGestureRecogniser
方法中使用的代码:
touchPoint.center = getPositionAlongTheLine(firstPoint: mainPoint1,
secondPoint: mainPoint2,
percentage: percentage)
其中 mainPoint1 和 mainPoint2 是侧面按钮。完美运行!
推荐阅读
- json - 如何使用 PL/SQL 将 JSON 数据解析成列?
- python - cython 在 cythonize 之后选择基于 Python 的实现
- video - Pafy 无法将 YouTube 视频读取为“webm”
- node.js - nodejs express-validator:仅在值存在时验证电子邮件格式
- laravel - 如何使用 laravel 发送多封邮件?
- vue.js - Vue Mixin - 处理数据属性
- javascript - 如何更改对象数组的特定属性?
- node.js - 即使使用 rollup-plugin-node-externals & @rollup/plugin-node-resolve,“未解决的依赖关系”
- android - systrace 中未显示自定义事件跟踪
- php - 如何在真实条件下获得返回值