如何让视图跟随我的手指移动?方向是从左到右

How to make a view follow my finger to move? Direction is from left to right

我想实现一个类似于滑块的功能。例如,按钮跟随手指从左向右移动。 像这样:

我应该使用 UISwipeGestureRecognizer 吗?还是 UIPanGestureRecognizer? 我使用了滑动手势,但我不知道如何更新框架。

var swipeRight = UISwipeGestureRecognizer(target: self, action: 
#selector(self.respondToSwipeGesture(sender:)))
swipeView.isUserInteractionEnabled = true
swipeView.addGestureRecognizer(swipeRight)

如果每次用户手指在屏幕上移动时将他们的手指存储在一个数组中,您就可以做到非常简单。我已经写了一些代码,你可以遵循。它很容易遵循并直接前进。 创建一个单一视图应用程序并放置此代码和 运行 应用程序并查看其结果。

import UIKit

class ViewController: UIViewController {

    let numViewPerRow = 15

    var cells = [String: UIView]()

    override func viewDidLoad() {
        super.viewDidLoad()

        let width = view.frame.width / CGFloat(numViewPerRow)

        let numViewPerColumn = Int(view.frame.height / width)

        for j in 0...numViewPerColumn {
            for i in 0...numViewPerRow {
                let cellView = UIView()
                cellView.backgroundColor = UIColor(red: .random(in: 0...1), green: .random(in: 0...1), blue: .random(in: 0...1), alpha: 1)
                cellView.frame = CGRect(x: CGFloat(i) * width, y: CGFloat(j) * width, width: width, height: width)
                cellView.layer.borderWidth = 0.5
                cellView.layer.borderColor = UIColor.black.cgColor
                view.addSubview(cellView)

                let key = "\(i)|\(j)"
                cells[key] = cellView
            }
        }

        view.addGestureRecognizer(UIPanGestureRecognizer(target: self, action: #selector(handlePan)))
    }

    var selectedCell: UIView?

    @objc func handlePan(gesture: UIPanGestureRecognizer) {
        let location = gesture.location(in: view)

        let width = view.frame.width / CGFloat(numViewPerRow)

        let i = Int(location.x / width)
        let j = Int(location.y / width)
        print(i, j)

        let key = "\(i)|\(j)"

        guard let cellView = cells[key] else { return }

        if selectedCell != cellView {
            UIView.animate(withDuration: 0.5, delay: 0, usingSpringWithDamping: 1, initialSpringVelocity: 1, options: .curveEaseOut, animations: {

                self.selectedCell?.layer.transform = CATransform3DIdentity

            }, completion: nil)
        }

        selectedCell = cellView

        view.bringSubviewToFront(cellView)

        UIView.animate(withDuration: 0.5, delay: 0, usingSpringWithDamping: 1, initialSpringVelocity: 1, options: .curveEaseOut, animations: {

            cellView.layer.transform = CATransform3DMakeScale(3, 3, 3)

        }, completion: nil)

        if gesture.state == .ended {

            UIView.animate(withDuration: 0.5, delay: 0.25, usingSpringWithDamping: 0.5, initialSpringVelocity: 0.5, options: .curveEaseOut, animations: {

                cellView.layer.transform = CATransform3DIdentity

            }, completion: { (_) in

            })
        }
    }
}

您可以将平移手势添加到要拖动的视图。拖动方法如下所示:

func draggedView(_ sender: UIPanGestureRecognizer) {
    let translation = sender.translation(in: self.view)
    draggableView.center = CGPoint(x: draggableView.center.x + translation.x, y: 
    draggableView.center.y)
    sender.setTranslation(CGPoint.zero, in: self.view)
}

您可以使用平移 x 或 y,具体取决于您是要水平移动视图还是垂直移动视图,或者两者都移动。