2011-09-20 10 views
11

orijinal dokunmatik Konum alma Bence bir UIPanGestureRecognizer eklendiğinden.
sorun dokunmatik bir tava jest olarak kabul edilmektedir zaman, birkaç puan Taşınacak sahip olduğunu, ve ben UIGestureRecognizerStateBegan devlet orijinal dokunmatik yerini ayıklayamazsınız. Bu durumda translationInView: (0,0) değeridir ve sonraki hamleler bu noktadan hesaplanır ve orijinalden değil.UIPanGestureRecognizer

jest tanıyıcı kendisinden orijinal konumunu ayıklamak için bir yol var mı, yoksa touchesBegan: yöntemini geçersiz gerekiyor?

+0

Bu bağlantı yardımcı olabilir - http://www.icodeblog.com/2010/10/14/working-with-uigesturerecognizers/ – Coffee

cevap

7

Sen jest tanıyıcı delegate ayarlayabilirsiniz ve ilk dokunuş almak için

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch 

uygulamalıdır. Eğer locationInView: yerine translationInView: kullanırsanız

+2

Bu da çalışır. Bununla birlikte, sonunda "dokunur" ifadesini kullandım:. Görünüşe göre, pan jest tanıyıcı aslında dokunma olayının başlangıçta başladığı bir ipucu içermiyor. – antalkerekes

1

mutlak koordinatları yerine nispi koordinatlarını alacak. Bir UIButton olabilir bu bakış geçici olarak çözmek için ... fikirlerini almak için tavayı başlamak zorunda ve bir - (IBAction)buttonWasTouched:(UIButton *)button forEvent:(UIEvent *)event şöyle "touch down" bağlı tetikleyebilir Ancak:

-(IBAction)shakeIntensityPanGesturebuttonWasTouched:(UIButton *)button forEvent:(UIEvent *)event 
{ 
    UITouch *touch = [[event allTouches] anyObject]; 
    CGPoint location = [touch locationInView:touch.view]; //touch location local cordinates 
} 
3

Sen uygulayarak bu sorunu çözebilir Orijinal temas noktasını kaydeden ve arayan kişiye uygun hale getiren özel bir PanGestureRecognizer.

Aynı zamanda, pan hareketi'u tetiklemek için taşınan mesafeyi kontrol etmek istediğimden bu rotaya gittim.

Bu durum için boğucu, ama mükemmel çalışıyor ve bu sesler daha zor değildir olabilir.

temas noktası koordinatlarını almak için, sadece çağrı: yerine

[sender touchPointInView:yourView] 

:

#import "PanGestureRecognizer.h" 
#import <UIKit/UIGestureRecognizerSubclass.h> 

@implementation PanGestureRecognizer 

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event 
{ 
    [super touchesBegan:touches withEvent:event]; 
    UITouch *touch = [touches anyObject]; 

    // touchPoint is defined as: @property (assign,nonatomic) CGPoint touchPoint; 

    self.touchPoint = [touch locationInView:nil]; 
} 

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event 
{ 
    [super touchesMoved:touches withEvent:event]; 
    UITouch *touch = [touches anyObject]; 
    CGPoint p = [touch locationInView:nil]; 

    // customize the pan distance threshold 

    CGFloat dx = fabs(p.x-self.touchPoint.x); 
    CGFloat dy = fabs(p.y-self.touchPoint.y); 

    if (dx > 2 || dy > 2) { 
     if (self.state == UIGestureRecognizerStatePossible) { 
      [self setState:UIGestureRecognizerStateBegan]; 
     } 
     else { 
      [self setState:UIGestureRecognizerStateChanged]; 
     } 
    } 
} 

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event 
{ 
    [super touchesEnded:touches withEvent:event]; 

    if (self.state == UIGestureRecognizerStateChanged) { 
     [self setState:UIGestureRecognizerStateEnded]; 
    } 
    else { 
     [self setState:UIGestureRecognizerStateCancelled]; 
    } 
} 

- (void) reset 
{ 
} 

// this returns the original touch point 

- (CGPoint) touchPointInView:(UIView *)view 
{ 
    CGPoint p = [view convertPoint:self.touchPoint fromView:nil]; 

    return p; 
} 

@end 
+0

Bunu nasıl kullanması gerektiğini anlamıyorum. –

0

Sen UIGestureRecognizerState

kullanabilirsiniz:

[sender locationInView:yourView] 

İşte PanGestureRecognizer.m için kod

- (void) onPanGesture:(UIPanGestureRecognizer *)sender { 
    if(sender.state == UIGestureRecognizerStateBegan) { 
     origin = [sender locationInView]; 
    } else { 
     current = [sender locationInView]; 
     // Initial touch stored in origin 
    } 
} 

Swift (imsi):

var state = recognizer.state 
if state == UIGestureRecognizerState.Began { 
    println(recognizer.locationInView(viewForGesture)) 
    //this will be the point of first touch 
} 
+0

Cevabınız için açıklama çeşit veriniz .. – Lal

+0

Hareket tanıyıcılar farklı durumları (.Began, .Ended, .Cancelled, vs ...) var. Tanıyıcı durumu olduğunda, (görünüm veya dokunma) konumunu alırsanız, ilk konumuna sahip olacaksınız. – Aiden

+0

Bu kodu, jest tarafından çağrılan işlevin içine yerleştirin.Yukarıdaki println çizgisi, dokunma/kaydırma başına yalnızca bir koordinat çifti basacaktır – Aiden