2011-05-04 60 views
1

我在屏幕上檢測到一個滑動,它的工作原理與我想要的一樣。 唯一的問題是,在測試中,我一遍又一遍地反覆滑動,至少有90%的時間或更多時間在響應,但是現在又一次沒有響應。touchesBegan並不總是迴應

我NSLog的一切都找到了罪魁禍首,並發現touchesBegan沒有得到檢測在這種情況發生的幾次。

這裏是我的代碼,雖然的touchesBegan甚至沒有得到調用,所以代碼不應該是事情:

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { 
    NSLog(@"touches began!"); 
    UITouch *touch = [touches anyObject]; 
    CGPoint thisTouch = [touch locationInView:self.view]; 

    touchstartedX = thisTouch.x; 
    touchstartedY = thisTouch.y; 
} 

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event { 

    CGPoint p = [[touches anyObject] locationInView:self.view]; 

    if ((abs(p.y - touchstartedY)) < 120) { 
     if ((p.x - touchstartedX) > 10) { 
      [self goPrevious]; 
     } else if ((p.x - touchstartedX) < -10) { 
      [self goNext]; 
     } 
    } else { NSLog(@"too much Y"); } 
} 

任何想法?

謝謝!

*用解決* 這裏編輯的代碼中,我結束了使用上dredful的建議探索UISwipeGestureRecognizer後:

在.H

UIViewController <UIGestureRecognizerDelegate> 

UISwipeGestureRecognizer *swipeLeft; 
UISwipeGestureRecognizer *swipeRight; 

@property (nonatomic, retain) UISwipeGestureRecognizer *swipeLeft; 
@property (nonatomic, retain) UISwipeGestureRecognizer *swipeRight; 
在.M

@synthesize swipeLeft, swipeRight; 

UIGestureRecognizer *recognizer; 
    recognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleSwipeFrom:)]; 
    self.swipeLeft = (UISwipeGestureRecognizer *)recognizer; 
    swipeLeft.direction = UISwipeGestureRecognizerDirectionLeft; 
    self.swipeLeft = (UISwipeGestureRecognizer *)recognizer; 
    [recognizer release]; 

    recognizer; 
    recognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleSwipeFrom:)]; 
    self.swipeRight = (UISwipeGestureRecognizer *)recognizer; 
    swipeRight.direction = UISwipeGestureRecognizerDirectionRight; 
    self.swipeRight = (UISwipeGestureRecognizer *)recognizer; 
    [recognizer release]; 

    [self.view addGestureRecognizer:swipeLeft];  
    [self.view addGestureRecognizer:swipeRight]; 

- (void)handleSwipeFrom:(UISwipeGestureRecognizer *)recognizer { 

    if (recognizer.direction == UISwipeGestureRecognizerDirectionLeft) { 
     [self goNext]; 
    } else { 
     [self goPrevious]; 
    } 
} 

- (void)dealloc 
{ 
    [swipeLeft release]; 
    [swipeRight release]; 
    [super dealloc]; 
} 

- (void)viewDidUnload 
{ 
    [super viewDidUnload]; 
    // Release any retained subviews of the main view. 
    // e.g. self.myOutlet = nil; 
    self.swipeLeft = nil; 
    self.swipeRight = nil; 
} 

回答

1

使用起來更容易蘋果的UISwipeGestureRecognizer

// add Swipe Left 
UISwipeGestureRecognizer *swipeLeft = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleSwipeLeft:)]; 
swipeLeft.direction = UISwipeGestureRecognizerDirectionLeft; 
[self.view addGestureRecognizer:swipeLeft]; 
[swipeLeft release]; 

// add Swipe Right 
UISwipeGestureRecognizer *swipeRight = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleSwipeRight:)]; 
swipeRight.direction = UISwipeGestureRecognizerDirectionRight; 
[self.view addGestureRecognizer:swipeRight]; 
[swipeRight release]; 


- (void)handleSwipeLeft:(UISwipeGestureRecognizer *)recognizer { 
    //do something 
} 

- (void)handleSwipeRight:(UISwipeGestureRecognizer *)recognizer { 
    //do something 
} 
+0

謝謝dredful - UISwipeGestureRecognizer工作得很好。 – RanLearns 2011-05-05 00:49:12

+0

很高興幫助。蘋果真的用手勢識別器打出了本壘打。讓我們的生活變得更輕鬆。 – dredful 2011-05-05 02:58:01