ios - 制作一个可拖动的 UIView,它可以捕捉到 iOS 中的边缘

标签 ios uiview draggable

我正在尝试制作一个可以在屏幕上拖动的 UIView,并且在释放时会捕捉到最近的边缘。

我已经解决了拖动问题 - 我正在使用 UILongPressGestureRecognizer,当它的状态结束时,我正在调用以下方法,其中 point 是 longPressGestureRecognizer 的 locationInView,rect 是包含 gesturerecognizer 和可拖动的 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;

除了当我放开 View 时它会捕捉到最近的角落外,这很有效。这很好,但不是我想要的。我希望它捕捉到最近的边缘,例如,如果我在它的点为 x:50 y:100(在 1136 x 640 的 super View 中)时放开 View ,我希望它捕捉到左侧(如x 值较小)但向下 100 像素而不是角。

有人可以告诉我怎么做吗?

谢谢

最佳答案

你的问题是你要确定是向左还是向右对齐,是向上对齐还是向下对齐。您正在寻找的是一种算法来决定向右、向左、顶部或底部对齐。

这是我的解决方案:

//put all of the distances between the point and rect edges in an array.
NSArray *values = @[@(point.x - rect.origin.x), @(rect.size.width - point.x), @(point.y - rect.origin.y), @(rect.size.height - point.y)];

//Find the smallest distance.
NSNumber *minimumValue = [NSNumber numberWithInteger:NSIntegerMax];
for(NSNumber *value in values){
    if([value intValue] < [minimumValue intValue]){
        minimumValue = value;
    }
}

if([minimumValue intValue] == point.x - rect.origin.x){
    //snap to the left
}
if([minimumValue intValue] == rect.size.width - point.x){
    //snap to the right
}
if([minimumValue intValue] == point.y - rect.origin.y){
    //snap to the top
}
if([minimumValue intValue] == rect.size.height - point.y){
    //snap to the bottom
}

它将所有值(上边距、右边距...)放在一个数组中,并遍历它们以找到最小值。您可以编写 if 语句的实现。我已经为你评论了每一个。我希望这能够帮到你。

编辑:

我更新了代码并修复了一些错误。当我测试它时,它对我有用。

关于ios - 制作一个可拖动的 UIView,它可以捕捉到 iOS 中的边缘,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24097326/

相关文章:

ios - 如何向 Collection View 单元格添加按钮

ios - 我应该在哪里准备数据?在 awakeFromNib、viewDidLoad 或其他东西中

ios - 在 Swift 中从其 UIViewController 访问 UIView

jquery-ui - jquery ui 可拖动元素出现在其他元素后面?

jquery - 可拖动元素有时会弹跳

android - 如何从 Google Drive 或 Onedrive 上传文件到 HTML5 Web App?

ios - 在 Singleton Objective C 中删除 NSNotificationCenter 的观察者

cocoa-touch - cocoa touch : Creating and Adding Custom View

ios - View 的底部和右侧约束是否定的?

vue.js - vue可拖动跟随v-chip-group选择