亚洲香蕉成人av网站在线观看_欧美精品成人91久久久久久久_久久久久久久久久久亚洲_热久久视久久精品18亚洲精品_国产精自产拍久久久久久_亚洲色图国产精品_91精品国产网站_中文字幕欧美日韩精品_国产精品久久久久久亚洲调教_国产精品久久一区_性夜试看影院91社区_97在线观看视频国产_68精品久久久久久欧美_欧美精品在线观看_国产精品一区二区久久精品_欧美老女人bb

首頁 > 系統 > 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開發頻道。
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
亚洲香蕉成人av网站在线观看_欧美精品成人91久久久久久久_久久久久久久久久久亚洲_热久久视久久精品18亚洲精品_国产精自产拍久久久久久_亚洲色图国产精品_91精品国产网站_中文字幕欧美日韩精品_国产精品久久久久久亚洲调教_国产精品久久一区_性夜试看影院91社区_97在线观看视频国产_68精品久久久久久欧美_欧美精品在线观看_国产精品一区二区久久精品_欧美老女人bb
上原亚衣av一区二区三区| 在线播放亚洲激情| 国产成人在线播放| 97在线视频国产| 欧美黄网免费在线观看| 欧美自拍视频在线观看| 久久国产色av| 青青青国产精品一区二区| 不卡毛片在线看| 欧美亚洲视频在线观看| 日韩视频免费观看| 国产女人18毛片水18精品| 亚洲欧美国产高清va在线播| 综合av色偷偷网| 亚洲国产精品va在线| 日韩av免费在线观看| 日韩欧美在线国产| 中文字幕亚洲欧美在线| 91成人在线观看国产| 国产精品视频网| 国产日韩欧美黄色| 国产精品久久在线观看| 91在线观看免费观看| 精品久久久久久中文字幕大豆网| 91欧美激情另类亚洲| 久久久最新网址| 91国内在线视频| 国产日韩精品入口| 少妇激情综合网| 日韩av在线网| 中文字幕一区日韩电影| 亚洲成人网av| 91国自产精品中文字幕亚洲| 亚洲最大福利网| 神马久久桃色视频| 欧美一区二区三区艳史| 91在线观看免费网站| 国产精品一区二区三区成人| 91精品国产电影| 欧美最近摘花xxxx摘花| 国产精品久久久久久久久久三级| 国产成人精品日本亚洲专区61| 国产精品福利片| 久久久久久一区二区三区| 久久精品国产一区二区电影| 欧美激情亚洲视频| 性欧美视频videos6一9| 中文字幕视频在线免费欧美日韩综合在线看| 热门国产精品亚洲第一区在线| 一区二区三区视频在线| 久久亚洲精品小早川怜子66| 亚洲精品一区av在线播放| 久久精品99久久久久久久久| 欧美日韩一二三四五区| 不卡毛片在线看| 国产成人97精品免费看片| 亚洲第一区在线| 亚洲精品美女在线观看播放| 国产精品福利观看| 成人国产精品日本在线| 久久高清视频免费| 亚洲精品一区二区网址| 久久中文精品视频| 亚洲国产成人精品久久久国产成人一区| 狠狠躁天天躁日日躁欧美| 欧美成人精品激情在线观看| 久久久视频在线| 亚洲福利精品在线| 亚洲香蕉伊综合在人在线视看| 国内精品免费午夜毛片| 亚洲女人天堂av| 青青草精品毛片| 国产日产欧美精品| 92看片淫黄大片看国产片| 欧美精品日韩www.p站| 久久精品国产欧美亚洲人人爽| 亚洲欧美自拍一区| 精品国内产的精品视频在线观看| 91精品国产91久久| 国产精品极品美女在线观看免费| 日韩中文在线中文网三级| 日韩欧美国产激情| 狠狠色噜噜狠狠狠狠97| 九九久久精品一区| 欧美多人乱p欧美4p久久| 亚洲视频免费一区| 色综合久久天天综线观看| 日韩免费看的电影电视剧大全| 成人信息集中地欧美| 狠狠躁夜夜躁久久躁别揉| 另类图片亚洲另类| 日本一区二区三区四区视频| 欧美电影在线播放| 亚洲精品之草原avav久久| 欧美国产日韩一区二区在线观看| 最新国产精品拍自在线播放| 91久久国产精品| 8090理伦午夜在线电影| 久久免费观看视频| 亚洲一区二区在线| 国产精品国模在线| 日韩美女毛茸茸| 亚洲乱码国产乱码精品精| 国产精品中文字幕在线| 久久久久久久久久久国产| 日韩免费av片在线观看| 国产精品免费视频久久久| 欧美高清理论片| 亚洲国产日韩欧美在线图片| 亚洲成人性视频| 久久99久久亚洲国产| 亚洲人精选亚洲人成在线| 欧美日韩激情网| 国产精品自产拍在线观看| 日韩av在线电影网| 亚洲精品网站在线播放gif| 亚洲欧美日韩精品| 国产精品视频99| 亚洲天堂av在线免费| 国产91在线播放九色快色| 国产亚洲精品一区二555| 国产精品久久av| 91最新在线免费观看| 亚洲在线第一页| 国产视频精品免费播放| 亚洲精品一区av在线播放| 久久成人这里只有精品| 久久夜色精品国产亚洲aⅴ| 国产美女久久精品香蕉69| 欧美国产在线视频| 最近中文字幕2019免费| 亚洲国产精品推荐| 97精品免费视频| 最近2019年日本中文免费字幕| 不卡在线观看电视剧完整版| 国产精品美女免费视频| 欧美在线欧美在线| 国产精品久久久久久网站| 亚洲精品自拍视频| 亚洲va欧美va国产综合剧情| 国产精品黄页免费高清在线观看| 日本不卡视频在线播放| 九色成人免费视频| 亚洲欧洲第一视频| 国产精品成人观看视频国产奇米| 日韩精品中文字幕在线观看| 欧美第一黄网免费网站| 中文字幕在线成人| 国产z一区二区三区| 国产欧美一区二区三区久久人妖| 91精品久久久久久久久不口人| 亚洲欧美在线看| www.亚洲免费视频| 精品呦交小u女在线| 国产亚洲精品久久久久久| 国产精品视频白浆免费视频| 一区二区三区视频免费| 亚洲爱爱爱爱爱| 久久天天躁狠狠躁夜夜爽蜜月| 欧美精品一二区| 国产97免费视| 国产精品福利在线观看网址| 韩国19禁主播vip福利视频| 亚洲石原莉奈一区二区在线观看|