您好,登錄后才能下訂單哦!
使用UIPageViewController怎么實現左右滑動界面?針對這個問題,這篇文章詳細介紹了相對應的分析和解答,希望可以幫助更多想解決這個問題的小伙伴找到更簡單易行的方法。
.h
#import <UIKit/UIKit.h> @interface ViewController : UIViewController @end
.m
#import "ViewController.h" #import "SubPage1ViewController.h" #import "SubPage2ViewController.h" @interface ViewController ()<UIPageViewControllerDataSource,UIPageViewControllerDelegate> @property NSArray *contentViewControllers; //ViewControllers @property (nonatomic, strong) UIPageViewController *pageViewController; @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; [self pageViewController]; } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } #pragma mark #pragma mark ----- UIPageViewControllerDataSource ----- - (UIViewController *)pageViewController:(UIPageViewController *)pageViewController viewControllerBeforeViewController:(UIViewController *)viewController { NSUInteger index = [self indexForViewController:viewController]; if (index == 0) { index = [self.contentViewControllers count] - 1; } else { index--; } return [self viewControllerAtIndex:index]; } - (UIViewController *)pageViewController:(UIPageViewController *)pageViewController viewControllerAfterViewController:(UIViewController *)viewController { NSUInteger index = [self indexForViewController:viewController]; index++; if (index == [self.contentViewControllers count]) { index = 0; } return [self viewControllerAtIndex:index]; } - (NSUInteger)indexForViewController:(UIViewController *)viewController { return [self.contentViewControllers indexOfObject:viewController]; } - (UIViewController *)viewControllerAtIndex:(NSUInteger)index { if (index > [self.contentViewControllers count]) { return nil; } UIViewController *vc = [self.contentViewControllers objectAtIndex:index]; return vc; } #pragma mark #pragma mark ----- UIPageViewControllerDelegate ----- - (void)pageViewController:(UIPageViewController *)pageViewController willTransitionToViewControllers:(NSArray *)pendingViewControllers { } - (void)pageViewController:(UIPageViewController *)pageViewController didFinishAnimating:(BOOL)finished previousViewControllers:(NSArray *)previousViewControllers transitionCompleted:(BOOL)completed { } #pragma mark #pragma mark Init - (UIPageViewController *)pageViewController { if(!_pageViewController) { NSDictionary *options =[NSDictionary dictionaryWithObject:[NSNumber numberWithInteger:UIPageViewControllerSpineLocationMin] forKey: UIPageViewControllerOptionSpineLocationKey]; _pageViewController = [[UIPageViewController alloc] initWithTransitionStyle:UIPageViewControllerTransitionStyleScroll navigationOrientation:UIPageViewControllerNavigationOrientationHorizontal options:options]; [[_pageViewController view] setFrame:[[self view] bounds]]; _contentViewControllers = [NSMutableArray arrayWithObjects:[SubPage1ViewController new],[SubPage2ViewController new],nil]; // 設置UIPageViewController的配置項 [_pageViewController setViewControllers:[NSArray arrayWithObjects:_contentViewControllers[0], nil] direction:UIPageViewControllerNavigationDirectionForward animated:NO completion:nil]; _pageViewController.delegate = self; _pageViewController.dataSource = self; [self addChildViewController:self.pageViewController]; [self.view addSubview:self.pageViewController.view]; } return _pageViewController; }
關于使用UIPageViewController怎么實現左右滑動界面問題的解答就分享到這里了,希望以上內容可以對大家有一定的幫助,如果你還有很多疑惑沒有解開,可以關注億速云行業資訊頻道了解更多相關知識。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。