[英]How to make a view follow my finger to move? Direction is from left to right
I want to implement a function similar to a slider. 我想实现类似于滑块的功能。 For example, the button follows the finger and moves from left to right.
例如,按钮跟随手指并从左向右移动。 Like this:
像这样:
Should I use UISwipeGestureRecognizer. 我应该使用UISwipeGestureRecognizer吗? Or UIPanGestureRecognizer? I used a swipe gesture but I don't know how to update the frame.
还是UIPanGestureRecognizer?我使用了滑动手势,但是我不知道如何更新框架。
var swipeRight = UISwipeGestureRecognizer(target: self, action:
#selector(self.respondToSwipeGesture(sender:)))
swipeView.isUserInteractionEnabled = true
swipeView.addGestureRecognizer(swipeRight)
You can do very easy if store the user's finger in an array every time they move there figure around the screen. 如果用户每次在屏幕上移动手指时将手指存储在一个阵列中,则可以非常轻松地完成操作。 I have written some code that you can follow.
我写了一些代码,您可以遵循。 It's very easy to follow and straight forward.
这很容易遵循和直截了当。 create a single view app and place this code and run the app and see what is the result of this.
创建一个单一视图的应用程序并放置此代码并运行该应用程序,然后查看其结果。
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
})
}
}
}
You can add the pan gesture to the view you want to drag. 您可以将平移手势添加到要拖动的视图中。 The method for drag looks like this:
拖动方法如下所示:
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)
}
You can use translation x or y depending if you want to move the view horizontally or vertically, or both. 您可以使用平移x或y,具体取决于您是想水平移动还是垂直移动视图,或同时移动这两者。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.