Я создал действительно простое приложение с controlPage с тремя изображениями.Каждый раз, когда я нажимаю на точки в основе, страницы меняются.Все работает.Код, который я использовал:
@implementation myShareViewController
@synthesize gestureStartPoint;
-(IBAction) changePage {
switch ([pageControl currentPage]) {
case 0:
NSLog(@"changePage: In case 0");
[view2 removeFromSuperview];
[view3 removeFromSuperview];
[[self view] addSubview:view1];
break;
case 1:
NSLog(@"changePage: In case 1");
[view1 removeFromSuperview];
[view3 removeFromSuperview];
[[self view] addSubview:view2];
break;
case 2:
NSLog(@"changePage: In case 2");
[view2 removeFromSuperview];
[view1 removeFromSuperview];
[[self view] addSubview:view3];
break;
default:
break;
}
}
Затем я добавил следующий код для получения жестов (пролистывания)
#pragma mark -
-(void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch = [touches anyObject];
gestureStartPoint = [touch locationInView:self.view];
}
-(void) touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch = [touches anyObject];
CGPoint currentPoisition = [touch locationInView:self.view];
CGFloat deltaX = fabsf(gestureStartPoint.x - currentPoisition.x);
CGFloat deltaY = fabsf(gestureStartPoint.y - currentPoisition.y);
if (deltaX >= kMinimumGestureLength && deltaY <= kMaximumVariance) {
NSLog(@">> Begin >> Hor. Swipe detected. We are now in the page: %i", [pageControl currentPage]);
[self changePage];
NSLog(@">> End >> Hor. Swipe detected. We are now in the page: %i", [pageControl currentPage]);
}
else if (deltaY >= kMinimumGestureLength && deltaX <= kMaximumVariance) {
NSLog(@"Ver. Swipe detected");
}
}
Теперь жесты распознаются нормально, но страницы не изменяются,
Обновление с решением
UISwipeGestureRecognizer *swipeRight = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleSwipeFrom:)];
[self.view addGestureRecognizer:swipeRight];
[swipeRight release];
и добавление:
- (void)handleSwipeFrom:(UISwipeGestureRecognizer *)recognizer
{
if (recognizer.direction == UISwipeGestureRecognizerDirectionRight)
{
// advance page
}
}