【问题标题】:How to make a view follow my finger to move? Direction is from left to right如何让视图跟随我的手指移动?方向是从左到右
【发布时间】:2019-12-18 16:23:08
【问题描述】:

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

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

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

【问题讨论】:

  • 使用UITouch 类。
  • 只需将其粘贴在滚动视图中即可。

标签: ios swift uiview uigesturerecognizer uitouch


【解决方案1】:

您可以将平移手势添加到要拖动的视图中。拖拽的方法是这样的:

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,具体取决于您要水平或垂直移动视图,或两者兼而有之。

【讨论】:

    【解决方案2】:

    如果用户的手指每次在屏幕上移动时都将其存储在一个数组中,那么您可以很容易地做到这一点。我写了一些你可以遵循的代码。这很容易遵循和直截了当。 创建一个单视图应用程序并放置此代码并运行该应用程序并查看其结果。

    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
    
                })
            }
        }
    }
    
    

    【讨论】:

    • 我需要实现的功能比较简单。只需要中间按钮或视图。从左向右拖动。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-09-05
    • 1970-01-01
    • 2012-01-30
    • 1970-01-01
    • 1970-01-01
    • 2016-03-20
    • 1970-01-01
    相关资源
    最近更新 更多