Các 'Trang-Based Application' mẫu cung cấp những 2 phương pháp:
- (NSUInteger)indexOfViewController:(DataViewController *)viewController;
một phương pháp cho việc tìm kiếm index cho một bộ điều khiển xem
- (DataViewController *)viewControllerAtIndex:(NSUInteger)index
một phương pháp cho instantiating một bộ điều khiển xem đưa ra một chỉ số .
Để tạo hoạt ảnh chính xác, bạn cần biết chỉ mục của bộ điều khiển chế độ xem hiện tại của mình. Các phương thức mẫu dựa trên trang hoàn toàn phù hợp với điều đó. Sau đó, bạn chỉ cần so sánh chỉ mục 'nhảy tới' của bạn và chỉ mục 'hiện tại' của bạn.
Dưới đây là một số mã để có được những ý tưởng:
- (void)jumpToPage:(NSInteger)page {
// find current index
DataViewController *currentViewController = (DataViewController *)[self.pageViewController.viewControllers lastObject];
NSUInteger index = [self indexOfViewController:currentViewController];
// choosing the correct direction
// if the 'current' is smaller than the 'jump to' page, then choose forward
// vice versa
UIPageViewControllerNavigationDirection direction;
if (index < page) {
direction = UIPageViewControllerNavigationDirectionForward;
} else {
direction = UIPageViewControllerNavigationDirectionReverse;
}
// choose view controllers according to the orientation
NSArray *viewControllers;
if (UIInterfaceOrientationIsPortrait(self.interfaceOrientation)) {
DataViewController *rightViewController = [self viewControllerAtIndex:page];
viewControllers = [NSArray arrayWithObject:rightViewController];
} else {
DataViewController *rightViewController = [self viewControllerAtIndex:page];
DataViewController *leftViewController = [self viewControllerAtIndex:page-1];
viewControllers = [NSArray arrayWithObjects:leftViewController, rightViewController, nil];
}
// fire the method which actually trigger the animation
[self.pageViewController setViewControllers:viewControllers
direction:direction
animated:YES
completion:nil];
}
Nguồn
2012-01-09 11:10:26
Ít nhất là trên iOS 6, '-setViewControllers: direction: animated: completion:' không được gọi nếu chuyển đổi được bắt đầu bằng cử chỉ. Tôi vừa thử nó. – jrc