根据苹果文件。Apple UIPanGestureRecognizer平移和滑动之间的区别如下:
UIPanGestureRecognizer是UIGestureRecognizer的具体子类用于查找平移(拖动)手势。用户平移视图时,必须在视图上按一个或多个手指。为该手势识别器实现动作方法的客户可以向其询问手势的当前转换和速度。
平移手势是连续的。当允许的最小手指数(minimumNumberOfTouches)移动到足以被视为平移时,它开始(开始)。当至少按下最小手指数时手指移动时,它会更改(更改)。抬起所有手指时,结束(结束)。
此类的客户端可以在其操作方法中查询UIPanGestureRecognizer对象以获取手势的当前转换(translation(in :))和转换速度(velocity(in :))。他们可以指定应将其坐标系用于平移和速度值的视图。客户还可以将翻译重置为所需的值。
Swift 3 UIPanGestureRecognizer演示示例:- 资源链接
import UIKit
class ViewController: UIViewController {
// this records our circle's center for use as an offset while dragging
var circleCenter: CGPoint!
override func viewDidLoad() {
super.viewDidLoad()
// Add a draggable view
let circle = UIView(frame: CGRect(x: 0.0, y: 0.0, width: 100.0, height: 100.0))
circle.center = self.view.center
circle.layer.cornerRadius = 50.0
circle.backgroundColor = UIColor.green()
// add pan gesture recognizer to
circle.addGestureRecognizer(UIPanGestureRecognizer(target: self, action: #selector(self.dragCircle)))
self.view.addSubview(circle)
}
func dragCircle(gesture: UIPanGestureRecognizer) {
let target = gesture.view!
switch gesture.state {
case .began, .ended:
circleCenter = target.center
case .changed:
let translation = gesture.translation(in: self.view)
target.center = CGPoint(x: circleCenter!.x + translation.x, y: circleCenter!.y + translation.y)
default: break
}
}
}
根据苹果文件。苹果UITapGestureRecognizer
UITapGestureRecognizer是UIGestureRecognizer的具体子类用于查找单次或多次点击。为了识别手势,指定数量的手指必须轻击视图多次。
尽管轻击是离散的手势,但是对于手势识别器的每种状态,轻击都是离散的。因此,关联的动作消息在手势开始时发送,并针对每个中间状态发送,直到(包括)手势的结束状态为止。因此,处理轻击手势的代码应测试手势的状态。
Swift 3 UITapGestureRecognizer演示示例 资源链接
override func viewDidLoad() {
super.viewDidLoad()
let tap = UITapGestureRecognizer(target: self, action: #selector(doubleTapped))
tap.numberOfTapsRequired = 2
view.addGestureRecognizer(tap)
}
func doubleTapped() {
// do something cool here
print("Test TapGesture")
}
识别器的示例图像