美文网首页程序员iOS程序猿
DWIntrosPage 简单定制引导页

DWIntrosPage 简单定制引导页

作者: Dwyane_Coding | 来源:发表于2018-09-14 11:08 被阅读21次
    源码在底下

    前言:

    该框架代码非常简单,仅需几行代码即可用于项目。
    1、目前支持动态图(GIF)、静态图,未来还会支持视频、缓存、横向等。
    2、页面切换时带渐变效果,体验感更强
    3、支持最后一页右移消失和前面页面直接跳过
    4、支持额外自定义,显示页面底下dots以及skip按钮与否。
    5、更低的内存占用、CPU占用
    6、支持直接使用图片名字,框架自动识别图片类型
    7、支持cocopods安装以及手动拖进项目

    1、使用方式:

    - (UIViewController *)setupDynamicVC {
        
        DWIntrosPageContentViewController *page1 = [DWIntrosPageContentViewController introsPageWithBackgroundImageWithName:@"gif01"];
        DWIntrosPageContentViewController *page2 = [DWIntrosPageContentViewController introsPageWithBackgroundImageWithName:@"gif02"];
        DWIntrosPageContentViewController *page3 = [DWIntrosPageContentViewController introsPageWithBackgroundImageWithName:@"gif03"];
        DWIntrosPagesViewController *introsPage = [DWIntrosPagesViewController dwIntrosPagesWithPageArray:@[page1, page2, page3]];
        __weak typeof(self) weakSelf = self;
        introsPage.skipButtonClickedBlock = ^{
            NSLog(@"clicked skip button");
            [weakSelf setupHomeVC];
        };
        return introsPage;
    }
    

    上面为使用静态图方式,动态图方式与此大同小异,故不多说。

    2、框架目录

    该框架主要有两大类:

    • DWIntrosPageContentViewController引导页的每一个详情页面
    • DWIntrosPagesViewController相当于引导页的容器,承载引导页的每一个详情页面。

    下面摘取部分代码
    DWIntrosPageContentViewController

    + (instancetype)introsPageWithBackgroundImageWithName:(NSString *)imgName {
        if ([imgName.pathExtension isEqualToString:@"jpg"] || [imgName.pathExtension isEqualToString:@"png"]) {
            return [[self alloc] initWithBackgroundImageWithName:imgName];
        }
        NSData *data = [self gainFullImageWithName:imgName];
        if ([_imagePathExtension((__bridge CFDataRef)(data)) isEqualToString:@"GIF"]) {
            return [[self alloc] initWithBackgroundGIFWithData:data];
        }else if([_imagePathExtension((__bridge CFDataRef)(data)) isEqualToString:@"JPG"]) {
            return [[self alloc] initWithBackgroundNormalImageWithData:data];
        }
        return [[self alloc] initWithBackgroundImageWithName:imgName];
    }
    

    设置每一页的详情,不同类型不同设置方式

    + (NSData *)gainFullImageWithName:(NSString *)name {
        if (name.length == 0) return nil;
        
        NSString *res = name.stringByDeletingPathExtension;
        NSString *ext = name.pathExtension;
        NSString *path = nil;
        NSArray *exts = ext.length > 0 ? @[ext] : @[@"", @"png", @"jpeg", @"jpg", @"gif", @"webp", @"apng"];
        for (NSString *e in exts) {
            path = [[NSBundle mainBundle] pathForResource:res ofType:e];
            if (path) break;
        }
        NSData *data = [NSData dataWithContentsOfFile:path];
        if (data.length == 0) return nil;
        return data;
    
    }
    

    判断图片是否有后缀,有后缀直接使用,没有后缀则尝试加上后缀名去[NSBundle mainBundle]寻找

    DWIntrosPagesViewController

    #pragma mark ------ UIPageViewControllerDelegate && DataSource ------
    - (UIViewController *)pageViewController:(UIPageViewController *)pageViewController viewControllerBeforeViewController:(UIViewController *)viewController {
       
        if (viewController == [self.viewControllers firstObject]) {
            return nil; // Return 'nil' to indicate that no more progress can be made in the given direction. (表示已经不能继续往左)
        }else {
            NSInteger lastPageIndex = [self.viewControllers indexOfObject:viewController] - 1;
            NSLog(@"%ld", (long)lastPageIndex);
            return [self.viewControllers objectAtIndex:lastPageIndex];
        }
    }
    
    - (UIViewController *)pageViewController:(UIPageViewController *)pageViewController viewControllerAfterViewController:(UIViewController *)viewController {
        if (viewController == [self.viewControllers lastObject]) {
            return nil; // (表示已经不能继续往右)
        }else {
            NSInteger nextPageIndex = [self.viewControllers indexOfObject:viewController] + 1;
    //        NSLog(@"%ld", (long)nextPageIndex);
            return self.viewControllers[nextPageIndex];
        }
    }
    

    判断控制器数组中的控制器,如果是首页或者尾页,则返回nil

    - (void)pageViewController:(UIPageViewController *)pageViewController didFinishAnimating:(BOOL)finished previousViewControllers:(NSArray<UIViewController *> *)previousViewControllers transitionCompleted:(BOOL)completed {
        // We need to confirm the transition completed and to do other things.
        // 确保转换完成才改变底下的小圆点
        if (completed) {
            _currentPage = [pageViewController.viewControllers lastObject];
            NSInteger currentIndex = [self.viewControllers indexOfObject:_currentPage];
            [self.pageControl setCurrentPage:currentIndex];
        }
    }
    

    等页面切换完成后才切换底下的dots

    #pragma mark ------ changeAlpha ------
    - (void)changeAlphaWithRatio:(CGFloat)ratio scrollView:(UIScrollView *)scrollView {
    
        if (ratio == 0) {
            return;
        }
       
        // figure out alpha
        // next page alpha equals to the ratio
        CGFloat nextPageAlpha = ratio;
        CGFloat currentPageAlpha = 1 - ratio;
        
        // warning: don't mix up the order of the two sentences
        //注意: 不要弄乱下面两句顺序
        [self.nextPage updateAlpha:nextPageAlpha];
        [self.currentPage updateAlpha:currentPageAlpha];
        
        // change the alpha of skip button and pageControl dots
        if (_nextPage == [self.viewControllers lastObject] ) {
            self.skipButton.alpha = currentPageAlpha;
            self.pageControl.alpha = currentPageAlpha;
        }
        if (_currentPage == [self.viewControllers lastObject]) {
            self.skipButton.alpha = nextPageAlpha;
            self.pageControl.alpha = nextPageAlpha;
            // The last page will not show the skill button and page dots.
            // 最后一页往右也不能出现下面两个按钮
            if ((scrollView.contentOffset.x > self.historyX)) {
                self.skipButton.alpha = 0;
                self.pageControl.alpha = 0;
                if (scrollView.contentOffset.x - self.historyX > 45) {
                    [self skipButtonCliked];
                }
            }
        }
    
    }
    
    

    根据移动的偏移值,对切换中的前后两个页面进行透明度的改变,从而达到渐变效果。

    最后

    整体而言,代码非常简单,请各位大佬轻拍。

    代码地址

    相关文章

      网友评论

        本文标题:DWIntrosPage 简单定制引导页

        本文链接:https://www.haomeiwen.com/subject/emdbgftx.html