DWIntrosPage 簡單定製引導頁

weixin_34148340發表於2018-09-14

前言:

該框架程式碼非常簡單,僅需幾行程式碼即可用於專案。 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];
            }
        }
    }

}

複製程式碼

根據移動的偏移值,對切換中的前後兩個頁面進行透明度的改變,從而達到漸變效果。

最後

整體而言,程式碼非常簡單,請各位大佬輕拍。

程式碼地址

相關文章