iphone - 沿 Y 轴的 UIView 平移手势

标签 iphone swift uiview uigesturerecognizer gesture

我有一个 UIView,我可以沿着 Y 轴拖放它,它会捕捉到 2 个位置中的 1 个。问题是我必须将它拖到 Y 轴上的某个点上才能使其卡入到位。当您尝试快速滑动但没有超过特定的 Y 轴时,这感觉很笨拙,它会快速回到起始位置。

我想要的目标是根据用户在释放 UIView 时滑动的方向,使其卡入 2 个位置中的 1 个位置。

@objc func panGesture(recognizer: UIPanGestureRecognizer) {
    let window = UIApplication.shared.keyWindow
    let translation = recognizer.translation(in: self)
    let currentY = self.frame.minY
    let partialY = (window?.frame.height)!-194
    let halfWayPoint = ((window?.frame.height)!/2)-40

    self.frame = CGRect(x: 0, y: currentY + translation.y, width: self.frame.width, height: self.frame.height)
    recognizer.setTranslation(CGPoint.zero, in: self)

    switch recognizer.state {

    case .changed:

        // Prevent scrolling up past y:0
        if currentY <= 0
        {
            self.frame = CGRect(x: 0, y: 0, width: self.frame.width, height: self.frame.height)
        }

    case .ended:

        // Snap to 80 (y) (expanded)
        if currentY < 80 || currentY > 80 && currentY < halfWayPoint
        {
            UIView.animate(withDuration:0.62, delay: 0.0, usingSpringWithDamping: 0.62, initialSpringVelocity: 0.5, options: .curveEaseInOut, animations:
                {
                    recognizer.view!.frame = CGRect(x: 0, y: 80, width: self.frame.width, height: self.frame.height)
            }, completion: { (true) in
                self.isExpanded = true
            })
        }

        // Snap back to partial (y) (original position)
        if currentY > partialY || currentY < partialY && currentY > halfWayPoint
        {
            UIView.animate(withDuration:0.62, delay: 0.0, usingSpringWithDamping: 0.62, initialSpringVelocity: 0.5, options: .curveEaseInOut, animations:
                {
                    recognizer.view!.frame = CGRect(x: 0, y: partialY, width: self.frame.width, height: self.frame.height)
            }, completion: {(true) in
                self.isExpanded = false
            })
        }

    default:
        print("Default")
    }

如您所见,halfWayPoint 是 View 必须位于上方或下方的点,以确定它捕捉到的位置。这是无效的,我想根据用户拖动 View 的方向(向上或向下)来执行此操作。

最佳答案

我建议你使用 UIPanGestureRecognizers velicoty(in:)方法。 它为您提供滑动速度(以点/秒为单位)。

如果你只是将它添加到 currentY(最好用一些常数缩放),你会得到那种轻弹的感觉,并且手势会感觉更自然。

只需更换 let currentY = self.frame.minYlet currentY = self.frame.minY + recognizer.velocity(in: nil).y * someConstant someConstant 直到你得到你想要的感觉。

关于iphone - 沿 Y 轴的 UIView 平移手势,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50462798/

相关文章:

iphone - 如何减小从 cam 捕获的图像尺寸

ios - Rx swift : Return a new observable with an error

ios - 如何让 List of String 的每个元素都可以点击?

objective-c - 如何检测以编程方式生成的 UIView 的旋转

iphone - 如何在SDK模拟器中模拟iPhone应用升级过程

ios - 当 UIViewController 呈现 UIViewControllerAnimatedTransitioning 并在 iOS8 中旋转时,UITabBarController 布局被破坏

javascript - HTML 中的 iPhone 表单元素

ios - 可调整大小 View 的圆角

ios - 快速自动布局+尺寸等级问题

iphone - 将 NIB 文件加载到 UIView 时遇到问题