touchesBegan并不总是回应

时间:2011-05-04 02:11:42

标签: iphone touch swipe touchesbegan

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

我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;

in .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 个答案:

答案 0 :(得分: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
}