我正在使用UIPageViewContoller创建一种类似于书本翻页的体验。我的书的页面比iPhone屏幕的全宽要窄18px,并且固定在屏幕的左侧。然后,我的UIPageViewController视图的框架被设置为这些页面的框架大小(宽度:302,高度:460)。我这样做是为了给人一种书有多个页面的效果,让页面看起来像是从当前可见页面的边缘开始,就像iBooks应用程序中的体验一样。
我遇到的问题是,如果有人试图通过从屏幕的最右侧平移来翻页,超过302px点,平移手势不会被UIPageViewController捕获,页面也不会翻转。我看过很多用户尝试以这种方式翻页,所以我想在不改变UI设计的情况下修复这种体验。
我的想法是,我可以从UIPageViewController之外的区域获取UIPanGesture,并将其传递给UIPageViewController。我已经使用一个图像视图作为整个视图的背景,成功地捕获了平移手势,但我不知道如何将该手势传递给UIPageViewController来处理翻页。
- (void) viewDidLoad {
...
// Add a swipe gesture recognizer to grab page flip swipes that start from the far right of the screen, past the edge of the book page
self.panGesture = [[[UIPanGestureRecognizer alloc] initWithTarget:self action:nil] autorelease];
[self.panGesture setDelegate:self];
[self.iv_background addGestureRecognizer:self.panGesture];
//enable gesture events on the background image
[self.iv_background setUserInteractionEnabled:YES];
...
}
#pragma mark - UIGestureRecognizer Delegates
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch {
// test if our control subview is on-screen
if (self.pageController.view.superview != nil) {
if (gestureRecognizer == self.panGesture) {
// we touched background of the BookViewController, pass the pan to the UIPageViewController
[self.pageController.view touchesBegan:[NSSet setWithObject:touch] withEvent:UIEventTypeTouches];
return YES; // handle the touch
}
}
return YES; // handle the touch
}
发布于 2012-06-07 21:35:34
UIPageViewController有一个gestureRecognizers属性。文档似乎准确地描述了您正在寻找的内容:
gestureRecognizers
配置为处理用户交互的UIGestureRecognizer对象数组。(只读)
@property(非原子,只读) NSArray *手势识别器
讨论
这些手势识别器最初附加到页面视图控制器的层次结构中的视图。更改用户可以使用手势导航的屏幕区域,这些手势可以放置在另一个视图上。
可用性
在iOS 5.0及更高版本中可用。在中声明
UIPageViewController.h
https://stackoverflow.com/questions/9204247
复制