【发布时间】:2014-07-28 15:19:30
【问题描述】:
我正在尝试制作一个可以在屏幕上拖动的 UIView,并且在释放时会捕捉到最近的边缘。
我已经解决了拖动问题 - 我正在使用 UILongPressGestureRecognizer,当它的状态结束时,我正在调用以下方法,其中 point 是 longPressGestureRecognizer 的 locationInView,rect 是包含手势识别器和可拖动的 UIView。此方法返回一个 CGPoint,然后我将其设置为可拖动 UIView 的中心。
- (CGPoint)closestPointForPoint:(CGPoint)point toSnapToInRect:(CGRect)rect {
CGPoint closestPoint = point;
/// Left/Right
if ((point.x - rect.origin.x) > (rect.size.width - point.x)) {
/// Point is closer to right side of frame, update the closestPoint's x.
closestPoint.x = rect.size.width - (self.draggableView.frame.size.width / 2) - draggableViewMargin;
}
else {
/// Point is closer to left side of frame, update the closestPoint's x.
closestPoint.x = rect.origin.x + (self.draggableView.frame.size.width / 2) + draggableViewMargin;
}
/// Top/Bottom
if ((point.y - rect.origin.y) > (rect.size.height - point.y)) {
/// Point is closer to top of frame, update the closestPoint's y.
closestPoint.y = rect.size.height - (self.draggableView.frame.size.height / 2) - draggableViewMargin;
}
else {
/// Point is closer to bottom of frame, update the closestPoint's y.
closestPoint.y = rect.origin.y + (self.draggableView.frame.size.height / 2) + draggableViewMargin;
}
return closestPoint;
}
这可行,只是当我放开视图时,它会捕捉到最近的角落。这很好,但不是我想要的。我希望它捕捉到最近的边缘,例如,如果我在它的点为 x: 50 y:100 时放开视图(在 1136 x 640 的超级视图中),我希望它捕捉到左侧(如x 值更小)但向下 100 像素而不是角落。
有人可以告诉我怎么做吗?
谢谢
【问题讨论】:
-
任何答案对你有用吗?