2011-03-03 23 views
54

ठीक है तो मैं मल्टी-टच जेस्चर को कैप्चर करने के लिए सूर्य के नीचे बस हर विकल्प पर देख रहा हूं, और अंततः मैं पूर्ण सर्कल आया हूं और यूआईपीएनजीस्टर रिकॉग्नाइज़र पर वापस आ गया हूं।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); 
} 
    } 

मैं सिर्फ बिंदु जहां मैं मूल्यों के बीच मतभेदों को ट्रैक करने के लिए कोशिश कर रहा शुरू करने के लिए जा रहा हूँ करने के लिए मिल गया है ... कोशिश करते हैं और बताओ जो रास्ता वे कर रहे हैं करने के लिए पैनिंग

+0

आप विधि है कि आपके पैन इशारा पहचानकर्ता आग के कार्यान्वयन पोस्ट कर सकते हैं? –

उत्तर

161

UIPanGestureRecognizer पर आप इशारा करते समय उंगलियों की गति प्राप्त करने के लिए -velocityInView: का उपयोग कर सकते हैं।

आप उदाहरण के लिए, एक पैन सही पर एक बात और एक पैन पर एक बात छोड़ करना चाहता था, तो आप की तरह कुछ कर सकता है:

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

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

आप सचमुच में के रूप में एक उलट पता लगाने के लिए, चाहते हैं

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

गुणा और बात जोड़ने के एक छोटे से अजीब लग सकता है: तुम कर सकते हो - जो भी दिशा हो सकता है कि - आप एक पुराने एक के लिए एक नया वेग तुलना और अगर यह सिर्फ विपरीत दिशा में है देखना चाहता हूँ। यह वास्तव में एक डॉट उत्पाद है, लेकिन बाकी आश्वासन दिया है कि यह एक सकारात्मक संख्या होगी यदि इशारे एक ही दिशा में हैं, तो 0 पर नीचे जा रहे हैं यदि वे बिल्कुल सही कोण पर हैं और फिर विपरीत संख्या में हैं तो वे विपरीत हैं दिशा। आप एक ऊर्ध्वाधर केवल खींचें चाहते हैं, आप x और y स्विच कर सकते हैं

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 
} 

:

+0

वाह! कर्तव्य की कॉल के ऊपर और परे रास्ता। वह जवाब बिल्कुल वही है जो मुझे चाहिए !!! धन्यवाद टॉमी! –

+0

बस जो मैं खोज रहा था! धन्यवाद! :) – Amit

+0

कोड का बहुत बड़ा कोड! –

7

यहाँ एक आसान से पहले इशारा पहचानकर्ता शुरू होता है पता लगाने के लिए है।

4

सेर्गेई कैटानियुक से यह कोड मेरे लिए बेहतर काम करता है। 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 
     } 


    } 
} 

// एक्सटेंशन

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