I created a really simple app with a controlPage with three images. Everytime I click on the dots at the base the pages changes. Everything works. The code I used is
@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;
}
}
Then I added the following code to get the gestures (swipes)
#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");
}
}
Now the gestures are recoginzed alright, but the pages don’t changes.
Update with solution
UISwipeGestureRecognizer *swipeRight = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleSwipeFrom:)];
[self.view addGestureRecognizer:swipeRight];
[swipeRight release];
and then add a:
- (void)handleSwipeFrom:(UISwipeGestureRecognizer *)recognizer
{
if (recognizer.direction == UISwipeGestureRecognizerDirectionRight)
{
// advance page
}
}
So I assume in your touchesMoved callback you are seeing something like:
This is because you aren’t actually telling the PageControl to change pages in this code. When you are calling changePages, you are telling the view to update to reflect the current state of the PageControl.
To have this work you will need to increment or decrement the currentPage property of the PageControl based on the user swipe.