麻豆小视频在线观看_中文黄色一级片_久久久成人精品_成片免费观看视频大全_午夜精品久久久久久久99热浪潮_成人一区二区三区四区

首頁 > 系統 > iOS > 正文

iOS使用pageViewController實現多視圖滑動切換

2019-10-21 18:40:51
字體:
來源:轉載
供稿:網友

本文實例為大家分享了pageViewController實現多視圖(控制器)滑動切換的具體代碼,供大家參考,具體內容如下

先看一下效果動畫

iOS,pageViewController,滑動切換iOS,pageViewController,滑動切換

類似的界面做過不少,在幾個APP中都有用到過,再次之前不了解uipageViewController 曾經的思路有兩個現在想想都覺得繁瑣。

之前的思路1:使用嵌套,collectionview嵌套,每個item中添加內容

之前的思路2:使用scrollview 在上面創建一個一個的controller 實現左右滑動

這兩個思路無疑是可以實現的,并且可以實現每個頁面的重用,滑動等都可,唯獨一點不好就是當停留在第一頁的時候,點擊標題欄第五頁,那么平移的過程就是第一頁到第五頁,所有的頁面從屏幕快速閃過,并且看到現在很多APP都是這樣的。在此之前我是用的思路2,為了避免跨頁面切換出現的中間幾個頁面閃過的過程,直接把平移動畫關閉了。直到使用了uipageViewController,趕緊把項目中的給換掉了

代碼不多150行以內

#import "ViewController.h"/// 當前controller#import "MyViewController.h" /// 復用的controller 適用于每個控制器布局相同的情況下,,布局不同就創建不同的controller添加進來#import "TitleCollectionViewCell.h"/// 標題欄使用的collectionviewcell@interface ViewController ()<UIPageViewControllerDelegate,UIPageViewControllerDataSource,UICollectionViewDelegate,UICollectionViewDataSource>{ //// 記錄當前頁 當前標題位置 NSInteger ld_currentIndex;}@property (nonatomic, strong) UIPageViewController *pageViewController;@property (nonatomic, strong) NSMutableArray *controllersArr;/// 控制器數組@property (nonatomic, strong) NSMutableArray *titleArray; /// 標題數組@property (nonatomic, strong) UICollectionView *titleCollectionView; /// 標題collectionview@end@implementation ViewController- (void)viewDidLoad { [super viewDidLoad]; self.view.backgroundColor = [UIColor whiteColor]; self.navigationController.navigationBar.translucent = NO; self.controllersArr = [NSMutableArray array]; self.titleArray = [NSMutableArray array]; //// 如果controller布局相同則循環創建MyViewController 添加進數組,,如果controller 布局不同 那就創建多個不同controller依次添加數組 for (int i = 0; i < 10; i++) {  MyViewController *con = [[MyViewController alloc]init];  [self.controllersArr addObject:con];  NSString *str = [NSString stringWithFormat:@"第 %d 頁", i+1];  con.titlestring = str;  [self.titleArray addObject:str]; } [self createCollectionView]; [self createPageViewController]; [self setTheFirstPage];}/// 創建標題collectionview- (void)createCollectionView{ UICollectionViewFlowLayout *lay = [[UICollectionViewFlowLayout alloc] init]; lay.itemSize = CGSizeMake(60, 30); lay.minimumLineSpacing = 0; lay.minimumInteritemSpacing = 0; lay.scrollDirection = UICollectionViewScrollDirectionHorizontal; self.titleCollectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 34, 375, 30) collectionViewLayout:lay]; self.titleCollectionView.showsHorizontalScrollIndicator = NO; self.titleCollectionView.backgroundColor = [UIColor whiteColor]; self.titleCollectionView.delegate = self; self.titleCollectionView.dataSource = self; [self.titleCollectionView registerClass:[TitleCollectionViewCell class] forCellWithReuseIdentifier:@"titleReuse"]; [self.navigationController.view addSubview:self.titleCollectionView];}//// 標題collectionview的協議方法- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{ return self.titleArray.count;}- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath { TitleCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"titleReuse" forIndexPath:indexPath]; cell.titleLabel.text = self.titleArray[indexPath.row]; if (indexPath.row == ld_currentIndex) {  cell.titleLabel.textColor = [UIColor orangeColor]; }else{  cell.titleLabel.textColor = [UIColor blackColor]; } return cell;}//// 點擊標題左右切換視圖控制器------------再也不用看到好幾個中間頁面從屏幕快速閃過了------- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{ UIViewController *vc = [self.controllersArr objectAtIndex:indexPath.row]; if (indexPath.row > ld_currentIndex) {  [self.pageViewController setViewControllers:@[vc] direction:UIPageViewControllerNavigationDirectionForward animated:YES completion:^(BOOL finished) {  }]; } else {  [self.pageViewController setViewControllers:@[vc] direction:UIPageViewControllerNavigationDirectionReverse animated:YES completion:^(BOOL finished) {  }]; } ld_currentIndex = indexPath.row; NSIndexPath *path = [NSIndexPath indexPathForRow:ld_currentIndex inSection:0]; [self.titleCollectionView scrollToItemAtIndexPath:path atScrollPosition:UICollectionViewScrollPositionCenteredHorizontally animated:YES]; [self.titleCollectionView reloadData];}/// 創建pageViewController- (void)createPageViewController { NSDictionary *option = [NSDictionary dictionaryWithObject:[NSNumber numberWithInteger:0] forKey:UIPageViewControllerOptionInterPageSpacingKey]; _pageViewController = [[UIPageViewController alloc]initWithTransitionStyle:UIPageViewControllerTransitionStyleScroll navigationOrientation:UIPageViewControllerNavigationOrientationHorizontal options:option]; _pageViewController.delegate = self; _pageViewController.dataSource = self; [self addChildViewController:_pageViewController]; [self.view addSubview:_pageViewController.view];}/// 展示上一頁- (nullable UIViewController *)pageViewController:(UIPageViewController *)pageViewController viewControllerBeforeViewController:(UIViewController *)viewController { NSInteger index = [self.controllersArr indexOfObject:viewController]; if (index == 0 || (index == NSNotFound)) {  return nil; } index--; return [self.controllersArr objectAtIndex:index];}/// 展示下一頁- (nullable UIViewController *)pageViewController:(UIPageViewController *)pageViewController viewControllerAfterViewController:(UIViewController *)viewController { NSInteger index = [self.controllersArr indexOfObject:viewController]; if (index == self.controllersArr.count - 1 || (index == NSNotFound)) {  return nil; } index++; return [self.controllersArr objectAtIndex:index];}/// 將要滑動切換的時候- (void)pageViewController:(UIPageViewController *)pageViewController willTransitionToViewControllers:(NSArray<UIViewController *> *)pendingViewControllers { UIViewController *nextVC = [pendingViewControllers firstObject]; NSInteger index = [self.controllersArr indexOfObject:nextVC]; ld_currentIndex = index;}/// 滑動結束后- (void)pageViewController:(UIPageViewController *)pageViewController didFinishAnimating:(BOOL)finished previousViewControllers:(NSArray<UIViewController *> *)previousViewControllers transitionCompleted:(BOOL)completed { if (completed) {  NSIndexPath *path = [NSIndexPath indexPathForRow:ld_currentIndex inSection:0];  [self.titleCollectionView scrollToItemAtIndexPath:path atScrollPosition:UICollectionViewScrollPositionCenteredHorizontally animated:YES];  [self.titleCollectionView reloadData];  NSLog(@">>>>>>>>> %ld", (long)ld_currentIndex); } }/// 設置默認顯示的是哪個頁面(controller)- (void)setTheFirstPage{ UIViewController *vc = [self.controllersArr objectAtIndex:ld_currentIndex]; [self.pageViewController setViewControllers:@[vc] direction:UIPageViewControllerNavigationDirectionReverse animated:YES completion:nil];}- (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated.}TitleCollectionViewCell@implementation TitleCollectionViewCell- (instancetype)initWithFrame:(CGRect)frame{ self = [super initWithFrame:frame]; if (self) {  [self createView]; } return self;}- (void)createView{ self.titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, self.contentView.frame.size.width, self.contentView.frame.size.height)]; [self.contentView addSubview:self.titleLabel]; self.titleLabel.font = [UIFont systemFontOfSize:14];}@end

demo分享:pageViewController實現多視圖滑動切換

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持VEVB武林網。


注:相關教程知識閱讀請移步到IOS開發頻道。
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 国产成年人网站 | 成人福利视频 | 国产成人精品免高潮在线观看 | 精品乱码久久久久 | xxxxhd73国产| 黄色大片网站在线观看 | 精品中文字幕在线播放 | 91福利影视 | 99在线免费观看视频 | 成人毛片100免费观看 | 国产亚洲精品视频中文字幕 | 国产精品成人一区二区三区电影毛片 | jizzjizzjizz少妇| 国产99页| 成人aaaa免费全部观看 | 九九热精品在线 | 国产在线一级视频 | 成人毛片在线免费看 | 美国av片在线观看 | 羞羞的视频在线 | 可以看逼的视频 | 国产一级毛片不卡 | 国产成人精品免高潮在线观看 | 超碰一区 | 欧美性受ⅹ╳╳╳黑人a性爽 | 免费黄色大片在线观看 | 久久精片 | 日日做夜夜操 | 黄www片| 黑人一级片视频 | 福利在线影院 | 日本精品网 | 久久这| 欧美成人精品一区二区三区 | 性aaa | 永久免费黄色大片 | v11av在线视频成人 | 一本色道久久综合狠狠躁篇适合什么人看 | 日本欧美一区二区三区视频麻豆 | 青青草成人影视 | 蜜桃免费在线 |