1

我正在检测屏幕上的滑动,它完全按照我想要的方式工作。唯一的问题是,在测试中,我一遍又一遍地滑动,至少 90% 或更多的时间它在响应,但时不时地没有响应。

我 NSLog'd 一切以找到罪魁祸首,并发现 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;
}
4

1 回答 1

1

使用Apple的更容易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
}
于 2011-05-04T02:43:52.940 回答