2011-09-20 196 views
11

我的看法添加到UIPanGestureRecognizer
問題在於,當觸摸被識別爲平移手勢時,必須移動幾個點,並且我無法提取UIGestureRecognizerStateBegan狀態下的原始觸摸位置。 在這種狀態下,translationInView:是(0,0),並且從該點開始計算任何後續移動,而不是從原始計算。從UIPanGestureRecognizer獲取原始觸摸位置

有沒有辦法從手勢識別器本身提取原始位置,還是我需要覆蓋touchesBegan:方法?

+0

此鏈接可以幫助 - http://www.icodeblog.com/2010/10/14/working-with-uigesturerecognizers/ – Coffee

回答

7

您應該能夠設置爲delegate手勢識別器並實現

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

獲得初始觸摸。

+2

這工作了。然而,最後我使用了'touchesBegan:'。看起來,平移手勢識別器真的不知道觸摸事件最初開始的位置。 – antalkerekes

1

如果您使用locationInView:而不是translationInView:,您將獲得絕對座標而不是相對座標。然而,你必須啓動盤來獲得輸入...若要解決此視圖可以是一個UIButton,你可以觸發一個- (IBAction)buttonWasTouched:(UIButton *)button forEvent:(UIEvent *)event連接到"touch down"像這樣:

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

您可以通過實現解決這個問題自定義PanGestureRecognizer可保存原始觸摸點並使其可供調用者使用。

我走了這條路線,因爲我也想控制距離移動觸發平移手勢

這可能是你的情況矯枉過正,但作品完美,並不難聽起來。

爲了得到接觸點的座標,您只要致電:

[sender touchPointInView:yourView] 

代替:

[sender locationInView:yourView] 

下面是PanGestureRecognizer.m代碼:

#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

我不明白應該如何使用它。 –

0

可以使用UIGestureRecognizerState

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

斯威夫特(ISH):

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

請提供一些解釋你的答案.. – Lal

+0

手勢識別器有不同的狀態(.Began,.Ended,.Cancelled,等...)。如果您在識別器狀態爲時發現(查看或觸摸)位置。開始您將獲得其第一個位置。 – Aiden

+0

將此代碼置於由手勢調用的函數內。上面的println行將只打印每個觸摸/平移一個座標對 – Aiden