防止可拖动视图脱离屏幕



我有一个可拖动的按钮,我不希望用户能够拖动视图,以便两侧被屏幕边缘切断。

override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) {
        if let touch = touches.first {
            let position = touch.location(in: self.superview)
            self.center = CGPoint(x: position.x, y: position.y)
            }
        }

我正在这样做的方式是一列如果/其他陈述,请检查屏幕上允许的区域。

 let halfOfButton = CGFloat(85 / 2)
            let heightOfTopBar = CGFloat(55) + halfOfButton
            let widthOfScreen = CGFloat((self.superview?.frame.width)!) - halfOfButton
            let heightOfScreen = CGFloat((self.superview?.frame.height)!) - heightOfTopBar
            let heightOfBotBar = CGFloat((self.superview?.frame.height)! - heightOfTopBar)

            if position.x > halfOfButton && position.x < widthOfScreen {
                self.center = CGPoint(x: position.x, y: position.y)
            } else {
                if position.x < halfOfButton {
                self.center = CGPoint(x: halfOfButton, y: position.y)
                } else {
                      self.center = CGPoint(x: widthOfScreen, y: position.y)
                }
            }
            if position.y > heightOfTopBar && position.y < heightOfScreen && position.x > halfOfButton && position.x < widthOfScreen {
                self.center = CGPoint(x: position.x, y: position.y)
            } else {
                if position.y < heightOfTopBar {
                    self.center = CGPoint(x: position.x, y: heightOfTopBar)
                } else {
                    self.center = CGPoint(x: position.x, y: heightOfScreen)
                }
            }
        }

有更好的方法可以实现这一目标吗?上面的代码甚至还没有完全工作。它在逻辑中有很多缺陷,我在说:"如果y还可以,那么只需允许您的触摸中心作为按钮的中心"即使y可能还可以,x也不是。

我可以以这种方式弄清楚它的逻辑,但是肯定有一种更好的方法?

尝试这样的东西:

override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) {
if let touch = touches.first {
    let position = touch.location(in: self.superview)
    var x:CGFloat = position.x
    var y:CGFloat = position.y
    let parentWidth = button.superView!.frame.size.width
    let parentHeight = button.superView!.frame.size.height
    let width = button.frame.size.width / 2
    let height = button.frame.size.height / 2
    x = max(width, x)
    x = min(x, parentWidth - width)
    y = max(height, y)
    y = min(y, parentHeight - height)

    self.center = CGPoint(x: x, y: y)
}

}

我处理了此问题,并以这种方法解决了。

首先,要说明按钮的大小,而不想夹住它的部分,我创建了一个视图(在此示例中,我们称其为 containerView),该视图放置在您希望按钮出现在的区域上。此观点需要从监督片段中降低其边际利润,以使其顶部&amp;底部边距是按钮高度的一半,它是领先的&amp;尾边是按钮宽度的一半。

通过设置该设置,现在可以用内置功能包含它的运动是相当微不足道的,该功能可以提供更好的性能而无需if语句。

override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) {
    super.touchesMoved(touches, with: event)
    guard let touch = touches.first else {
        return
    }
    let location = touch.location(in: containerView)
    if containerView.point(inside: location, with: event) {
        button.center = location
    }
}

现在,根据所使用的坐标系,使用此技术的位置将无法正确定位该按钮。如果确实发生了,则应该能够通过在将其分配给button.center属性之前修改位置值来解决它。

修改应是根据您的领导&amp;向右移动或向下移动。最高边缘。理想情况下,您将使用故事板约束为此,然后可以链接到控制器,允许您直接获取这些值进行调整。

最新更新