欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

iOS使用pageViewController實(shí)現(xiàn)多視圖滑動(dòng)切換

 更新時(shí)間:2018年06月30日 12:13:35   作者:flg_iOS  
這篇文章主要為大家詳細(xì)介紹了iOS使用pageViewController實(shí)現(xiàn)多視圖滑動(dòng)切換,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

本文實(shí)例為大家分享了pageViewController實(shí)現(xiàn)多視圖(控制器)滑動(dòng)切換的具體代碼,供大家參考,具體內(nèi)容如下

先看一下效果動(dòng)畫

類似的界面做過(guò)不少,在幾個(gè)APP中都有用到過(guò),再次之前不了解uipageViewController 曾經(jīng)的思路有兩個(gè)現(xiàn)在想想都覺得繁瑣。

之前的思路1:使用嵌套,collectionview嵌套,每個(gè)item中添加內(nèi)容

之前的思路2:使用scrollview 在上面創(chuàng)建一個(gè)一個(gè)的controller 實(shí)現(xiàn)左右滑動(dòng)

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

代碼不多150行以內(nèi)

#import "ViewController.h"http:/// 當(dāng)前controller
#import "MyViewController.h" /// 復(fù)用的controller 適用于每個(gè)控制器布局相同的情況下,,布局不同就創(chuàng)建不同的controller添加進(jìn)來(lái)
#import "TitleCollectionViewCell.h"http:/// 標(biāo)題欄使用的collectionviewcell

@interface ViewController ()<UIPageViewControllerDelegate,UIPageViewControllerDataSource,UICollectionViewDelegate,UICollectionViewDataSource>{

 //// 記錄當(dāng)前頁(yè) 當(dāng)前標(biāo)題位置

 NSInteger ld_currentIndex;

}

@property (nonatomic, strong) UIPageViewController *pageViewController;
@property (nonatomic, strong) NSMutableArray *controllersArr;/// 控制器數(shù)組
@property (nonatomic, strong) NSMutableArray *titleArray; /// 標(biāo)題數(shù)組
@property (nonatomic, strong) UICollectionView *titleCollectionView; /// 標(biāo)題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布局相同則循環(huán)創(chuàng)建MyViewController 添加進(jìn)數(shù)組,,如果controller 布局不同 那就創(chuàng)建多個(gè)不同controller依次添加數(shù)組
 for (int i = 0; i < 10; i++) {
 MyViewController *con = [[MyViewController alloc]init];
 [self.controllersArr addObject:con];
 NSString *str = [NSString stringWithFormat:@"第 %d 頁(yè)", i+1];
 con.titlestring = str;
 [self.titleArray addObject:str];

 }
 [self createCollectionView];
 [self createPageViewController];
 [self setTheFirstPage];

}



/// 創(chuàng)建標(biāo)題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];


}

//// 標(biāo)題collectionview的協(xié)議方法

- (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;

}

//// 點(diǎn)擊標(biāo)題左右切換視圖控制器------------再也不用看到好幾個(gè)中間頁(yè)面從屏幕快速閃過(guò)了------
- (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];

}



/// 創(chuàng)建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];

}

/// 展示上一頁(yè)
- (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];

}

/// 展示下一頁(yè)
- (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];

}

/// 將要滑動(dòng)切換的時(shí)候
- (void)pageViewController:(UIPageViewController *)pageViewController willTransitionToViewControllers:(NSArray<UIViewController *> *)pendingViewControllers {
 UIViewController *nextVC = [pendingViewControllers firstObject];
 NSInteger index = [self.controllersArr indexOfObject:nextVC];
 ld_currentIndex = index;

}

/// 滑動(dòng)結(jié)束后
- (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);

 } 

}

/// 設(shè)置默認(rèn)顯示的是哪個(gè)頁(yè)面(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實(shí)現(xiàn)多視圖滑動(dòng)切換

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • 實(shí)例講解iOS中的CATransition轉(zhuǎn)場(chǎng)動(dòng)畫使用

    實(shí)例講解iOS中的CATransition轉(zhuǎn)場(chǎng)動(dòng)畫使用

    CATransition類為應(yīng)用程序的轉(zhuǎn)場(chǎng)動(dòng)畫提供了很多可控制參數(shù),接下來(lái)我們就以幾個(gè)實(shí)例講解iOS中的CATransition轉(zhuǎn)場(chǎng)動(dòng)畫使用,需要的朋友可以參考下
    2016-06-06
  • iOS App開發(fā)中的UISegmentedControl分段組件用法總結(jié)

    iOS App開發(fā)中的UISegmentedControl分段組件用法總結(jié)

    UISegmentedControl主要被用來(lái)制作分頁(yè)按鈕或添加跳轉(zhuǎn)到不同位置的標(biāo)簽,這里我們就來(lái)看一下iOS App開發(fā)中的UISegmentedControl分段組件用法總結(jié),需要的朋友可以參考下
    2016-06-06
  • iOS自學(xué)筆記之XIB的使用教程

    iOS自學(xué)筆記之XIB的使用教程

    本篇文章主要介紹了iOS自學(xué)筆記之XIB的使用教程,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧
    2017-01-01
  • iOS的UI開發(fā)中Modal的使用與主流應(yīng)用UI結(jié)構(gòu)介紹

    iOS的UI開發(fā)中Modal的使用與主流應(yīng)用UI結(jié)構(gòu)介紹

    這篇文章主要介紹了iOS的UI開發(fā)中Modal的使用與主流應(yīng)用UI結(jié)構(gòu),代碼基于傳統(tǒng)的Objective-C,需要的朋友可以參考下
    2015-12-12
  • iOS10推送教程詳解

    iOS10推送教程詳解

    這篇文章主要為大家詳細(xì)介紹了iOS10推送開發(fā)教程,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2016-09-09
  • iOS開發(fā)教程之單例使用問(wèn)題詳析

    iOS開發(fā)教程之單例使用問(wèn)題詳析

    這篇文章主要給大家介紹了關(guān)于iOS開發(fā)教程之單例使用問(wèn)題的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)各位iOS開發(fā)者們具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2019-04-04
  • iOS?xcconfig編寫示例教程

    iOS?xcconfig編寫示例教程

    這篇文章主要為大家介紹了iOS?xcconfig編寫示例教程,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2022-07-07
  • iOS App設(shè)計(jì)模式開發(fā)中策略模式的實(shí)現(xiàn)示例

    iOS App設(shè)計(jì)模式開發(fā)中策略模式的實(shí)現(xiàn)示例

    這篇文章主要介紹了iOS App設(shè)計(jì)模式開發(fā)中策略模式的實(shí)現(xiàn)示例,例子采用傳統(tǒng)的Objective-C語(yǔ)言編寫,需要的朋友可以參考下
    2016-03-03
  • iOS發(fā)送短信功能的實(shí)現(xiàn)代碼

    iOS發(fā)送短信功能的實(shí)現(xiàn)代碼

    本篇文章主要介紹了iOS發(fā)送短信功能的實(shí)現(xiàn)代碼,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧
    2017-08-08
  • IOS開發(fā)中的設(shè)計(jì)模式匯總

    IOS開發(fā)中的設(shè)計(jì)模式匯總

    在ios的程序開發(fā)中,經(jīng)常搞暈ios的開發(fā)模式,今天小編就給大家簡(jiǎn)單的總結(jié)一下,需要的的朋友參考下
    2017-03-03

最新評(píng)論