Bra*_* B. 58 iphone reverse capture pan uigesturerecognizer
好的,所以我一直在四处寻找太阳下用于捕捉多点触控手势的所有选项,我终于完成了一圈并回到了UIPanGestureRecognizer.
我想要的功能非常简单.我已经设置了一个双指平移手势,我希望能够根据我移动的像素数量来移动一些图像.我已经做得很好了,但我希望能够捕获平移手势是否已经反转.
有没有内置的方式,我只是没有看到回过头来做手势?我是否必须存储我的原始起点,然后跟踪终点,然后查看它们之后的移动位置,如果它小于初始终点,然后相应地反转?我可以看到工作,但我希望有一个更优雅的解决方案!
谢谢
编辑:
以下是识别器设置为触发的方法.它有点像黑客,但它有效:
-(void) throttle:(UIGestureRecognizer *) recognize{
throttleCounter ++;
if(throttleCounter == 6){
throttleCounter = 0;
[self nextPic:nil];
}
UIPanGestureRecognizer *panGesture = (UIPanGestureRecognizer *) recognize;
UIView *view = recognize.view;
if(panGesture.state == UIGestureRecognizerStateBegan){
CGPoint translation = [panGesture translationInView:view.superview];
NSLog(@"X: %f, Y:%f", translation.x, translation.y);
}else if(panGesture.state == UIGestureRecognizerStateEnded){
CGPoint translation = [panGesture translationInView:view.superview];
NSLog(@"X: %f, Y:%f", translation.x, translation.y);
}
}
Run Code Online (Sandbox Code Playgroud)
我刚刚开始尝试跟踪值之间的差异......试着告诉他们正在淘汰的方式
Tom*_*mmy 173
在UIPanGestureRecognizer上,您可以使用-velocityInView :来获取识别手势时手指的速度.
例如,如果你想在平底锅上做一件事而在平底锅上放一件东西,你可以这样做:
- (void)handleGesture:(UIPanGestureRecognizer *)gestureRecognizer
{
CGPoint velocity = [gestureRecognizer velocityInView:yourView];
if(velocity.x > 0)
{
NSLog(@"gesture went right");
}
else
{
NSLog(@"gesture went left");
}
}
Run Code Online (Sandbox Code Playgroud)
如果您确实想要检测反转,就像您想要将新速度与旧速度进行比较并查看它是否只是在相反的方向 - 无论哪个方向 - 您可以这样做:
// assuming lastGestureVelocity is a class variable...
- (void)handleGesture:(UIPanGestureRecognizer *)gestureRecognizer
{
CGPoint velocity = [gestureRecognizer velocityInView:yourView];
if(velocity.x*lastGestureVelocity.x + velocity.y*lastGestureVelocity.y > 0)
{
NSLog(@"gesture went in the same direction");
}
else
{
NSLog(@"gesture went in the opposite direction");
}
lastGestureVelocity = velocity;
}
Run Code Online (Sandbox Code Playgroud)
乘法和加法可能看起来有点奇怪.它实际上是一个点积,但请放心,如果手势在同一个方向,它将是一个正数,如果它们是正确的角度则下降到0,如果它们是相反的话则变为负数方向.
Sam*_*fes 11
在手势识别器开始之前,这里很容易检测到:
public override func gestureRecognizerShouldBegin(_ gestureRecognizer: UIGestureRecognizer) -> Bool {
guard let panRecognizer = gestureRecognizer as? UIPanGestureRecognizer else {
return super.gestureRecognizerShouldBegin(gestureRecognizer)
}
// Ensure it's a horizontal drag
let velocity = panRecognizer.velocity(in: self)
if abs(velocity.y) > abs(velocity.x) {
return false
}
return true
}
Run Code Online (Sandbox Code Playgroud)
如果您只想垂直拖动,可以切换x和y.
来自Serghei Catraniuc的代码对我来说效果更好. https://github.com/serp1412/LazyTransitions
func addPanGestureRecognizers() {
let panGesture = UIPanGestureRecognizer(target: self, action: #selector(respondToSwipeGesture(gesture:)))
self.view.addGestureRecognizer(panGesture)
}
func respondToSwipeGesture(gesture: UIGestureRecognizer){
if let swipeGesture = gesture as? UIPanGestureRecognizer{
switch gesture.state {
case .began:
print("began")
case .ended:
print("ended")
switch swipeGesture.direction{
case .rightToLeft:
print("rightToLeft")
case .leftToRight:
print("leftToRight")
case .topToBottom:
print("topToBottom")
case .bottomToTop:
print("bottomToTop")
default:
print("default")
}
default: break
}
}
}
Run Code Online (Sandbox Code Playgroud)
//扩展名
import Foundation
import UIKit
public enum UIPanGestureRecognizerDirection {
case undefined
case bottomToTop
case topToBottom
case rightToLeft
case leftToRight
}
public enum TransitionOrientation {
case unknown
case topToBottom
case bottomToTop
case leftToRight
case rightToLeft
}
extension UIPanGestureRecognizer {
public var direction: UIPanGestureRecognizerDirection {
let velocity = self.velocity(in: view)
let isVertical = fabs(velocity.y) > fabs(velocity.x)
var direction: UIPanGestureRecognizerDirection
if isVertical {
direction = velocity.y > 0 ? .topToBottom : .bottomToTop
} else {
direction = velocity.x > 0 ? .leftToRight : .rightToLeft
}
return direction
}
public func isQuickSwipe(for orientation: TransitionOrientation) -> Bool {
let velocity = self.velocity(in: view)
return isQuickSwipeForVelocity(velocity, for: orientation)
}
private func isQuickSwipeForVelocity(_ velocity: CGPoint, for orientation: TransitionOrientation) -> Bool {
switch orientation {
case .unknown : return false
case .topToBottom : return velocity.y > 1000
case .bottomToTop : return velocity.y < -1000
case .leftToRight : return velocity.x > 1000
case .rightToLeft : return velocity.x < -1000
}
}
}
extension UIPanGestureRecognizer {
typealias GestureHandlingTuple = (gesture: UIPanGestureRecognizer? , handle: (UIPanGestureRecognizer) -> ())
fileprivate static var handlers = [GestureHandlingTuple]()
public convenience init(gestureHandle: @escaping (UIPanGestureRecognizer) -> ()) {
self.init()
UIPanGestureRecognizer.cleanup()
set(gestureHandle: gestureHandle)
}
public func set(gestureHandle: @escaping (UIPanGestureRecognizer) -> ()) {
weak var weakSelf = self
let tuple = (weakSelf, gestureHandle)
UIPanGestureRecognizer.handlers.append(tuple)
addTarget(self, action: #selector(handleGesture))
}
fileprivate static func cleanup() {
handlers = handlers.filter { $0.0?.view != nil }
}
@objc private func handleGesture(_ gesture: UIPanGestureRecognizer) {
let handleTuples = UIPanGestureRecognizer.handlers.filter{ $0.gesture === self }
handleTuples.forEach { $0.handle(gesture)}
}
}
extension UIPanGestureRecognizerDirection {
public var orientation: TransitionOrientation {
switch self {
case .rightToLeft: return .rightToLeft
case .leftToRight: return .leftToRight
case .bottomToTop: return .bottomToTop
case .topToBottom: return .topToBottom
default: return .unknown
}
}
}
extension UIPanGestureRecognizerDirection {
public var isHorizontal: Bool {
switch self {
case .rightToLeft, .leftToRight:
return true
default:
return false
}
}
}
Run Code Online (Sandbox Code Playgroud)