2

我有麻煩UIPanGestureRecognizer工作時,我有我的手指移動,因爲它只是調用的選擇,我希望它保持通話選擇,即使我的手指是站在同一個地方。UIPanGestureRecognizer停止調用選擇

有在頂部的屏幕上一個4個對象,一個在右側,一個在左側和一個在底部。我在屏幕的中心有一個對象(這是我用panGesture移動的對象)。當這個物體接觸到其他物體時,我希望它給我一個日誌,當它接觸時它會起作用,但如果我將手指放在同一個地方,它會停下來給我日誌,如果我移動一點,它就會再次給我日誌。

反正我有可以保持在同一個地方打電話,即使我的手指的選擇?

這裏是一個代碼示例:

- (void)moveObject:(UIPanGestureRecognizer *)sender 
{ 
    CGPoint translation = [sender translationInView:self.limiteDirecional]; 
    [sender setTranslation:CGPointMake(0, 0) inView:self.limiteDirecional]; 

    CGPoint center = sender.view.center; 
    center.y += translation.y; 
    int yMin = 0; 
    int yMax = self.limiteDirecional.frame.size.height; 

    if (center.y < yMin || center.y > yMax) 
     return; 

    sender.view.center = center; 

    center.x += translation.x; 
    int xMin = self.limiteDirecional.frame.size.width; 
    int xMax = 0; 

    if (center.x > xMin || center.x < xMax) 
     return; 

    sender.view.center = center; 

    if (CGRectIntersectsRect(sender.view.frame,self.Top.frame)) { 
     NSLog(@"TOP");   
    } 

    if (CGRectIntersectsRect(sender.view.frame,self.Botton.frame)) { 
     NSLog(@"BOTTON"); 
    } 

    if (CGRectIntersectsRect(sender.view.frame,self.Right.frame)) { 
     NSLog(@"RIGHT"); 
    } 

    if (CGRectIntersectsRect(sender.view.frame,self.Left.frame)) { 
     NSLog(@" LEFT"); 
    } 

    if (sender.state == UIGestureRecognizerStateEnded) { 
     sender.view.center = CGPointMake(self.view.frame.size.width/2, self.view.frame.size.height/2); 
    } 
} 
+1

的手勢識別,本身不會那樣做。您可以使用計時器或'CADisplayLink'或其他類似的東西,但要提出解決方案,瞭解您要解決的問題很有用。當觸摸沒有變化時,通過獲取觸摸變化的通知來解決什麼問題? – Rob 2013-02-13 19:28:01

+0

對!這工作就像一個操縱桿,如果你在與「傳感器」應該將我的精靈接觸拖動「MainObject」,但如果我持有「MainObject」即使是觸摸「傳感器」停止移動我的精靈!我必須輕輕地保持移動... – rihurla 2013-02-13 20:02:33

回答

3

我不會完全按照你程序的邏輯,所以我會提供一個什麼樣的解決方案可能看當你想在連續的事件,如通用模板手勢的中間,無論用戶是否在移動他們的手指。希望您可以根據自己的目的調整這項技術。

這使用CADisplayLink,這被認爲是比以前使用NSTimer的技術更好的動畫技術。要使用CADisplayLink,不過,你需要add the needed framework,QuartzCore.framework,你的項目,如果您還沒有。還要注意的是,在我的手勢識別,我檢查的手勢的狀態,要知道我們是否正在開始一個手勢,一箇中間或結尾之一:

#import "ViewController.h" 
#import <QuartzCore/QuartzCore.h> 

@interface ViewController() 

@property (nonatomic, strong) CADisplayLink *displayLink; 
@property (nonatomic) CGPoint translationInView; 

@end 

@implementation ViewController 

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 

    UIPanGestureRecognizer *gesture = [[UIPanGestureRecognizer alloc] initWithTarget:self 
                       action:@selector(handleGesture:)]; 
    // I'm adding to the main view, but add it to whatever you want 
    [self.view addGestureRecognizer:gesture]; 
} 

- (void)startDisplayLink 
{ 
    self.displayLink = [CADisplayLink displayLinkWithTarget:self selector:@selector(handleDisplayLink:)]; 
    [self.displayLink addToRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode]; 
} 

- (void)stopDisplayLink 
{ 
    [self.displayLink invalidate]; 
    self.displayLink = nil; 
} 

- (void)handleDisplayLink:(CADisplayLink *)displayLink 
{ 
    NSLog(@"%s translationInView = %@", __FUNCTION__, NSStringFromCGPoint(self.translationInView)); 

    // Do here whatever you need to happen continuously while the user is in the 
    // middle of the gesture, whether their finger is moving or not. 
} 

- (void)handleGesture:(UIPanGestureRecognizer *)gesture 
{ 
    self.translationInView = [gesture translationInView:gesture.view]; 

    if (gesture.state == UIGestureRecognizerStateBegan) 
    { 
     [self startDisplayLink]; 

     // Do whatever other initialization stuff as the user starts the gesture 
     // (e.g. you might alter the appearance of the joystick to provide some 
     // visual feedback that they're controlling the joystick). 
    } 
    else if (gesture.state == UIGestureRecognizerStateChanged) 
    { 
     // Do here only that stuff that actually changes as the user is moving their 
     // finger in the middle of the gesture, but which you don't need to have 
     // repeatedly done while the user's finger is not moving (e.g. maybe the 
     // visual movement of the "joystick" control on the screen). 
    } 
    else if (gesture.state == UIGestureRecognizerStateEnded || 
      gesture.state == UIGestureRecognizerStateCancelled || 
      gesture.state == UIGestureRecognizerStateFailed) 
    { 
     [self stopDisplayLink]; 

     // Do whatever other cleanup you want to do when the user stops the gesture 
     // (e.g. maybe animating the moving of the joystick back to the center). 
    } 
} 
@end 

您也可以達到類似如果您還使用NSTimer,也會產生影響。無論什麼對你更好。