iOS引導(dǎo)頁封裝

插眼傳送

引導(dǎo)頁是App必不可少的功能之一;本Demo用簡(jiǎn)單有效的代碼,封裝引導(dǎo)頁,僅需3行代碼搞定,同時(shí)支持GIF圖片.

GuideView.h

引導(dǎo)頁GuideView : UIViewController,在.h文件中,暴露接口,支持自定義設(shè)置


@interface GuideView : UIViewController

/**
 引導(dǎo)頁圖片數(shù)組(gif圖 需要添加后綴名:icon.gif)
 */
@property (nonatomic,strong)NSArray *imageArray;

/**
 跳過按鈕是否顯示(默認(rèn)為NO)
 */
@property (nonatomic,assign)BOOL cancelButtonShow;

/**
 分頁控件是否顯示(默認(rèn)為NO)
 */
@property (nonatomic,assign)BOOL pageControlShow;

/**
 未選中圓點(diǎn)顏色
 */
@property (nonatomic,strong)UIColor *pageIndicatorColor;

/**
 選中圓點(diǎn)顏色
 */
@property (nonatomic,strong)UIColor *currentPageIndicatorColor;

/**
 設(shè)置引導(dǎo)頁圖片

 @param imageArray 引導(dǎo)頁數(shù)組
 @param rootController 根控制器
 */
- (void)showGuideViewWithImageArray:(NSArray *)imageArray WindowRootController:(UIViewController *)rootController;

@end

GuideView.m

利用SDWebImage加載GIF,導(dǎo)入#import "UIImage+GIF.h",
坑點(diǎn):sd_animatedGIFNamed 不能帶.gif 后綴否則只能加載第一張; 使用GCD倒計(jì)時(shí),解決滑動(dòng)時(shí),倒計(jì)時(shí)不準(zhǔn)確問題

#import "GuideView.h"
#import "UIImage+GIF.h"

#define K_Screen_width [UIScreen mainScreen].bounds.size.width
#define K_Screen_height [UIScreen mainScreen].bounds.size.height


@interface GuideView ()<UIScrollViewDelegate>

/**
 滾動(dòng)視圖
 */
@property (nonatomic,strong)UIScrollView *imageScrollView;
/**
 圓點(diǎn)
 */
@property (nonatomic,strong) UIPageControl *pageControl;

/**
 跳過按鈕
 */
@property (nonatomic,strong)UIButton *cancelButton;

/**
 跟控制器
 */
@property (nonatomic,strong)UIViewController *rootController;

@end

@implementation GuideView

- (void)viewDidLoad {
    [super viewDidLoad];
    [self createScrollView];
    [self createPageControl];
    [self createCancelButton];
   }

- (void)createScrollView
{
    _imageScrollView = [[UIScrollView alloc]initWithFrame:self.view.bounds];
    _imageScrollView.delegate = self;
    _imageScrollView.bounces = YES;
    _imageScrollView.pagingEnabled = YES;
    _imageScrollView.showsVerticalScrollIndicator = NO;
    _imageScrollView.showsHorizontalScrollIndicator = NO;
    _imageScrollView.backgroundColor = [UIColor whiteColor];
    _imageScrollView.contentSize = CGSizeMake(K_Screen_width *self.imageArray.count, K_Screen_height);
    [self.view addSubview:_imageScrollView];
    for (int i = 0; i < self.imageArray.count; i++) {
        NSString *imageName = self.imageArray[i];
        UIImageView *imageView = [[UIImageView alloc]init];
        imageView.userInteractionEnabled = YES;
        imageView.backgroundColor = [UIColor purpleColor];
        imageView.frame = CGRectMake(K_Screen_width * i, 0, K_Screen_width, K_Screen_height);
        [_imageScrollView addSubview:imageView];
        // 判斷是否為gif
        if ( [imageName.pathExtension.lowercaseString isEqualToString:@"gif"]) {
            // sd_animatedGIFNamed 不能帶.gif 后綴否則只能加載第一張
            // 過濾掉 .gif
            NSString *tureName = [imageName substringToIndex:imageName.length - 4];
            imageView.image = [UIImage sd_animatedGIFNamed:tureName];
        }else{
            imageView.image = [UIImage imageNamed:imageName];
        }
    }
}

- (void)createPageControl
{
    _pageControl = [[UIPageControl alloc]initWithFrame:CGRectMake(0, K_Screen_height - 80, K_Screen_width, 30)];
    _pageControl.hidden = _pageControlShow;
    _pageControl.pageIndicatorTintColor = _pageIndicatorColor;
    _pageControl.currentPageIndicatorTintColor = _currentPageIndicatorColor;
    _pageControl.numberOfPages = self.imageArray.count;
    [self.view addSubview:_pageControl];
}

- (void)createCancelButton
{
    _cancelButton = [UIButton buttonWithType:UIButtonTypeCustom];
    _cancelButton.layer.cornerRadius = 2;
    _cancelButton.hidden = _cancelButtonShow;
    _cancelButton.titleLabel.font = [UIFont systemFontOfSize:15];
    _cancelButton.backgroundColor = [[UIColor whiteColor] colorWithAlphaComponent:0.5];
    _cancelButton.frame = CGRectMake(K_Screen_width - 70, 20, 60, 20);
    [_cancelButton setTitle:@"跳過" forState:UIControlStateNormal];
    [_cancelButton addTarget:self action:@selector(cancelButtonAction:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:_cancelButton];
}
// 倒計(jì)時(shí)
- (void)startTimer
{
    __block NSInteger second = 10;
    //全局隊(duì)列    默認(rèn)優(yōu)先級(jí)
    dispatch_queue_t quene = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
    //定時(shí)器模式  事件源
    dispatch_source_t timer = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0, quene);
    //NSEC_PER_SEC是秒,*1是每秒
    dispatch_source_set_timer(timer, dispatch_walltime(NULL, 0), NSEC_PER_SEC * 1, 0);
    //設(shè)置響應(yīng)dispatch源事件的block,在dispatch源指定的隊(duì)列上運(yùn)行
    dispatch_source_set_event_handler(timer, ^{
        //回調(diào)主線程,在主線程中操作UI
        dispatch_async(dispatch_get_main_queue(), ^{
            if (second >= 0) {
                [_cancelButton setTitle:[NSString stringWithFormat:@"%lds跳過",second] forState:UIControlStateNormal];
                second--;
            }
            else
            {
                //這句話必須寫否則會(huì)出問題
                dispatch_source_cancel(timer);
                [_cancelButton setTitle:@"跳過" forState:UIControlStateNormal];
                    self.view.window.rootViewController = _rootController;
            }
        });
    });
    //啟動(dòng)源
    dispatch_resume(timer);
}

- (void)setImageArray:(NSArray *)imageArray
{
    _imageArray = imageArray;
}

- (void)setCancelButtonShow:(BOOL)cancelButtonShow
{
    _cancelButtonShow = cancelButtonShow;
    if (_cancelButtonShow){
          // 加載完畢開始倒計(jì)時(shí)
            [self startTimer];
      }
}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
    // pageControl 與 scrollView 聯(lián)動(dòng)
    CGFloat offsetWidth = scrollView.contentOffset.x;
    int pageNum = offsetWidth / [[UIScreen mainScreen] bounds].size.width;
    self.pageControl.currentPage = pageNum;
    if (scrollView.contentOffset.x >= scrollView.contentSize.width - K_Screen_width + 40) {
        self.view.window.rootViewController = _rootController;
    }
}

- (void)showGuideViewWithImageArray:(NSArray *)imageArray WindowRootController:(UIViewController *)rootController
{
    _imageArray = imageArray;
    _rootController = rootController;
}

- (void)cancelButtonAction:(UIButton *)sender
{
    self.view.window.rootViewController = _rootController;
}
@end

使用(重點(diǎn)來了)

判斷是否是第一次進(jìn)入,如果是第一次 就就如引導(dǎo)頁,不是就設(shè)置正常的根控制器(怎么判斷是否第一次進(jìn)入app 就不多說了)

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    self.window = [[UIWindow alloc]initWithFrame:[UIScreen mainScreen].bounds];
    self.window.backgroundColor = [UIColor whiteColor];
    [self.window makeKeyAndVisible];
    // if 條件 是 判斷是否是第一次進(jìn)入,如果是第一次 就就如引導(dǎo)頁,不是就設(shè)置正常的根控制器
    if (YES) {
        GuideView *vc = [[GuideView alloc]init];
        [vc showGuideViewWithImageArray:@[@"10.gif",@"1.png",@"2.png",@"3.png"] WindowRootController:[ViewController new]];
        self.window.rootViewController = vc;
    }else{
      /**-------------------你工程的根控制器--------------**/
    }
    return YES;
}
Demo.gif
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容