2011-03-03 38 views
54

Ok vì vậy tôi đã nhìn xung quanh chỉ là về mọi tùy chọn dưới ánh mặt trời để chụp cử chỉ đa cảm ứng, và cuối cùng tôi đã đến vòng tròn đầy đủ và quay lại với UIPanGestureRecognizer.Làm cách nào tôi có thể chụp được hướng nào đang được quét bằng UIPanGestureRecognizer?

Chức năng tôi muốn thực sự khá đơn giản. Tôi đã thiết lập cử chỉ xoay hai ngón tay và tôi muốn có thể phát ngẫu nhiên thông qua một số hình ảnh tùy thuộc vào số lượng pixel tôi di chuyển. Tôi có tất cả những gì làm việc ra được, nhưng tôi muốn để có thể nắm bắt nếu cử chỉ pan là REVERSED.

Có cách nào được xây dựng theo cách mà tôi không thấy để phát hiện trở lại trên cử chỉ không? Tôi có phải lưu trữ điểm xuất phát ban đầu của tôi, sau đó theo dõi điểm kết thúc, sau đó xem nơi họ di chuyển sau đó và se nếu nó ít hơn điểm kết thúc ban đầu và sau đó đảo ngược cho phù hợp? Tôi có thể thấy rằng làm việc, nhưng tôi hy vọng có một giải pháp thanh lịch hơn !!

Cảm ơn

EDIT:

Đây là phương pháp mà các nhận dạng được thiết lập để bắn. Đó là một chút của một hack, nhưng nó hoạt động:

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

Tôi vừa mới đến điểm mà tôi sẽ bắt đầu cố gắng theo dõi sự khác biệt giữa các giá trị ... để thử và cho biết cách chúng panning

+0

Bạn có thể gửi thực hiện các phương pháp mà pan bạn cử chỉ recognizer cháy? –

Trả lời

161

Trên UIPanGestureRecognizer bạn có thể sử dụng -velocityInView: để nhận được vận tốc của các ngón tay tại thời điểm cử chỉ được nhận dạng.

Nếu bạn muốn làm một điều về quyền chảo và một điều trên một chảo trái, ví dụ, bạn có thể làm một cái gì đó như:

- (void)handleGesture:(UIPanGestureRecognizer *)gestureRecognizer 
{ 
    CGPoint velocity = [gestureRecognizer velocityInView:yourView]; 

    if(velocity.x > 0) 
    { 
     NSLog(@"gesture went right"); 
    } 
    else 
    { 
     NSLog(@"gesture went left"); 
    } 
} 

Nếu bạn có nghĩa là muốn phát hiện một sự đảo ngược, như trong bạn muốn so sánh một vận tốc mới một cũ và xem nếu nó chỉ là theo hướng ngược lại - tùy theo hướng đó có thể - bạn có thể làm:

// 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; 
} 

các nhân và thêm điều có thể trông hơi kỳ quặc. Nó thực sự là một sản phẩm chấm, nhưng yên tâm nó sẽ là một số dương nếu các cử chỉ ở cùng hướng, đi xuống 0 nếu chúng chính xác ở góc phải và sau đó trở thành số âm nếu chúng ở đối diện phương hướng.

+0

Wow! Cách trên và vượt ra ngoài các cuộc gọi của nhiệm vụ. Câu trả lời đó chính xác là những gì tôi cần !!! Cảm ơn Tommmy! –

+0

Chỉ là những gì tôi đang tìm kiếm! Cảm ơn! :) – Amit

+0

Một chút mã! –

7

Dưới đây là một cách dễ dàng để phát hiện trước khi nhận dạng cử chỉ bắt đầu:

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 
} 

Nếu bạn muốn có một chỉ kéo dọc, bạn có thể chuyển đổi các xy.

4

Mã này từ Serghei Catraniuc đã làm việc tốt hơn cho tôi. 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 
     } 


    } 
} 

// Extensions

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 
     } 
    } 
} 
Các vấn đề liên quan