Я обнаруживаю пролистывание экрана, и оно работает именно так, как я хочу.
Единственное, во время тестирования я продолжаю перелистывать снова и снова и, по крайней мере, 90% времени или больше, когда он отвечает, но время от времени ответа нет.
Я нашел все, чтобы найти виновника, и обнаружил, что прикосновения Бегана вообще не обнаруживаются в те несколько раз, когда это происходит.
Вот мой код, хотя 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"); }
}
Есть идеи?
Спасибо!
* РЕДАКТИРОВАТЬ С РЕШЕНИЕМ *
Вот код, который я использовал после изучения UISwipeGestureRecognizer по предложению dredful:
в .ч:
UIViewController <UIGestureRecognizerDelegate>
UISwipeGestureRecognizer *swipeLeft;
UISwipeGestureRecognizer *swipeRight;
@property (nonatomic, retain) UISwipeGestureRecognizer *swipeLeft;
@property (nonatomic, retain) UISwipeGestureRecognizer *swipeRight;
в .м:
@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;
}