iOS 中的平移和滑动有什么区别?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9898627/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What is the difference between Pan and Swipe in iOS?
提问by James Raitsev
Sounds simple .. Hold the Trackpad, move the finger, release.. But somehow swipe is not being triggered (pan is triggered instead)
听起来很简单..按住触控板,移动手指,松开.. 但不知何故没有触发滑动(而是触发平移)
UISwipeGestureRecognizer *swipeGesture = [[UISwipeGestureRecognizer alloc]
initWithTarget:v action:@selector(handleSwipe:)];
swipeGesture.direction= UISwipeGestureRecognizerDirectionUp;
[v addGestureRecognizer:swipeGesture];
Pan is recognized by the above sequence instead.
Pan 被上述序列识别。
UIPanGestureRecognizer *panGesture = [[UIPanGestureRecognizer alloc]
initWithTarget:v action:@selector(handlePan:)];
[v addGestureRecognizer: panGesture];
If pan is commented, swipe is recognized by the same gesture .. With this, 2 questions:
如果 pan 被评论,滑动被相同的手势识别..有了这个,2个问题:
- What is the difference then between a pan and a swipe?
- How can one simulate a swipe on iPhone simulator?
- 那么平底锅和刷卡有什么区别?
- 如何在 iPhone 模拟器上模拟滑动?
回答by rickster
By definition, a swipe gesture is necessarily also a pan gesture -- both involve translational movement of touch points. The difference is in the recognizer semantics: a pan recognizer looks for the beginning of translational movement and continues to report movement in any direction over time, while a swipe recognizer makes an instantaneous decision as to whether the user's touches moved linearly in the required direction.
根据定义,滑动手势必然也是平移手势——两者都涉及触摸点的平移运动。区别在于识别器语义:平移识别器寻找平移运动的开始,并随着时间的推移继续报告任何方向的运动,而滑动识别器立即决定用户的触摸是否在所需方向上线性移动。
By default, no two recognizers will recognize the same gesture, so there's a conflict between pan and swipe. Most likely, your pan recognizer "wins" the conflict because its gesture is simpler / more general: A swipe is a pan but a pan may not be a swipe, so the pan recognizes first and excludes other recognizers.
默认情况下,没有两个识别器会识别相同的手势,因此平移和滑动之间存在冲突。最有可能的是,您的平移识别器“赢得”了冲突,因为它的手势更简单/更通用:滑动是平移,但平移可能不是滑动,因此平移首先识别并排除其他识别器。
You should be able to resolve this conflict using the delegate method gestureRecognizer:shouldRecognizeSimultaneouslyWithGestureRecognizer:
, or perhaps without delegation by making the pan recognizer depend on the swipe recognizer with requireGestureRecognizerToFail:
.
您应该能够使用委托方法解决此冲突gestureRecognizer:shouldRecognizeSimultaneouslyWithGestureRecognizer:
,或者通过使平移识别器依赖于滑动识别器而无需委托requireGestureRecognizerToFail:
。
With the conflict resolved, you should be able to simulate a one-finger swipe by quickly dragging the mouse. (Though as the mouse is more precise than your finger, it's a bit more finicky than doing the real thing on a device.) Two-finger pan/swipe can be done by holding the Option & Shift keys.
解决冲突后,您应该能够通过快速拖动鼠标来模拟单指滑动。(尽管鼠标比您的手指更精确,但它比在设备上做真实的事情要挑剔一些。)可以通过按住 Option 和 Shift 键来实现两指平移/滑动。
回答by Daya Kevin
Swipe Gesture will work when you drag your finger only in certain directions (swipe up,swipe down,swipe left, swipe right). For example swipeable cells in table view controller.
Pan Gesture will work when you drag your finger in any directions. You can give acceleration or deceleration to it. FOr example, moving a object from one place to another place or spinning a spinner..
当您仅在特定方向(向上滑动、向下滑动、向左滑动、向右滑动)拖动手指时,滑动手势将起作用。例如表视图控制器中的可滑动单元格。
当您向任何方向拖动手指时,平移手势将起作用。你可以给它加速或减速。例如,将物体从一个地方移动到另一个地方或旋转微调器。
回答by Rex
As per the apple document. Apple UIPanGestureRecognizerThe difference between a pan and a swipe as below:-
根据苹果文档。Apple UIPanGestureRecognizer平移和滑动之间的区别如下:-
UIPanGestureRecognizeris a concrete subclass of UIGestureRecognizerthat looks for panning (dragging) gestures. The user must be pressing one or more fingers on a view while they pan it. Clients implementing the action method for this gesture recognizer can ask it for the current translation and velocity of the gesture.
UIPanGestureRecognizer是UIGestureRecognizer的具体子类,用于寻找平移(拖动)手势。用户在平移视图时必须将一根或多根手指按在视图上。为这个手势识别器实现动作方法的客户端可以询问手势的当前平移和速度。
A panning gesture is continuous. It begins (began) when the minimum number of fingers allowed (minimumNumberOfTouches) has moved enough to be considered a pan. It changes (changed) when a finger moves while at least the minimum number of fingers are pressed down. It ends (ended) when all fingers are lifted.
平移手势是连续的。它开始(开始)时,允许手指的最小数量(minimumNumberOfTouches)已移动到足以被视为一个锅。当至少有最少数量的手指被按下时,当手指移动时它会改变(改变)。当所有手指都抬起时,它结束(结束)。
Clients of this class can, in their action methods, query the UIPanGestureRecognizer object for the current translation of the gesture (translation(in:)) and the velocity of the translation (velocity(in:)). They can specify the view whose coordinate system should be used for the translation and velocity values. Clients may also reset the translation to a desired value.
此类的客户端可以在其操作方法中查询 UIPanGestureRecognizer 对象以获取手势的当前翻译(translation(in:))和翻译速度( velocity(in:))。他们可以指定其坐标系应用于平移和速度值的视图。客户端还可以将转换重置为所需的值。
Swift 3 UIPanGestureRecognizer Demo Example:-Resource Link
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
}
}
}
As per the apple document. Apple UITapGestureRecognizer
根据苹果文档。苹果 UITapGestureRecognizer
UITapGestureRecognizeris a concrete subclass of UIGestureRecognizerthat looks for single or multiple taps. For the gesture to be recognized, the specified number of fingers must tap the view a specified number of times.
UITapGestureRecognizer是的具体子类UIGestureRecognizer看起来为单个或多个水龙头。要识别手势,指定数量的手指必须点击视图指定次数。
Although taps are discrete gestures, they are discrete for each state of the gesture recognizer; thus the associated action message is sent when the gesture begins and is sent for each intermediate state until (and including) the ending state of the gesture. Code that handles tap gestures should therefore test for the state of the gesture.
虽然点击是离散的手势,但它们对于手势识别器的每个状态都是离散的;因此,关联的动作消息在手势开始时发送,并针对每个中间状态发送,直到(并包括)手势的结束状态。因此,处理点击手势的代码应该测试手势的状态。
Swift 3 UITapGestureRecognizer Demo ExampleResource Link
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")
}
回答by JasmineOT
According to http://hammerjs.github.io/, I think the difference is:
根据http://hammerjs.github.io/,我认为区别在于:
- pan: move directions in the same big view
- swipe: switch between several views
- pan:在同一个大视图中移动方向
- 滑动:在多个视图之间切换
The gesture is the same, both use one finger and move.
手势是一样的,都用一根手指移动。