我用一个带有三个图像的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
}
}
最佳答案
因此,我假设在您的touchesMoved回调中您看到的是:
开始>>贺。侦测到滑动。我们现在在页面中:0
结束>>贺。侦测到滑动。我们现在在页面中:0
这是因为您实际上并未在此代码中告诉PageControl更改页面。调用changePages时,是在告诉视图进行更新以反映PageControl的当前状态。
若要进行此工作,您将需要根据用户滑动来增加或减少PageControl的currentPage属性。