1. 程式人生 > >ios新手引導頁(oc 和swift)

ios新手引導頁(oc 和swift)

在第一次安裝App, 或者更新App的時候, 常常會出現一個引導介面, 昨天晚上花了點時間, 寫了個引導頁,太晚就沒發出來, 實現一個引導頁其實並不困難. 請看程式碼
點我下載Swift的Demo
點我下載OC的demo

我們用NSUserDefaults類來判斷程式是不是第一次啟動或是否更新,在

    //顯示新手引導圖
    
    if ([GuideOutgoingServer isNeedLoadGuidePage]) {
        _guidePageInterface = [GuideOutgoingFactory getGuidePageManager];
        [_guidePageInterface loadGuideData];
        [_guidePageInterface showGuideInView:self.window];
   
    }

在GuidePageManager中,我們用UIScrollView來裝載我們的引導頁:

#import "GuidePageManager.h"
#import <UIKit/UIKit.h>
#import "GuideLayout.h"

#define SCREEN_WIDTH ([[UIScreen mainScreen] bounds].size.width)

#define SCREEN_HEIGHT ([[UIScreen mainScreen] bounds].size.height)

static CGFloat const kCMBCAdViewsAutoScrollDuration = 5.0f;
typedef void(^CMBCGuideAniFinished)(void);

@interface GuidePageManager ()<UIScrollViewDelegate>

@property (nonatomic,strong) UIScrollView *scrollView;

@property (nonatomic,strong) UIPageControl *pageCon;
@property (nonatomic, strong) NSArray *images;
@property (nonatomic,strong) NSTimer *timer;
@property(strong,nonatomic)UIButton *nextButton;
@property (copy,nonatomic) CMBCGuideAniFinished complete;

@end

@implementation GuidePageManager

- (instancetype)init
{
    self = [super init];
    if (self) {
        
        [self p_sharedInit];
    }
    return self;
}

- (void)dealloc {
    
    //銷燬定時器
    [self stopScroll];
    
}

#pragma mark - Private Method
- (void)p_sharedInit {
    
    UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:[GuideLayout getGuideScrollViewFrame]];
    scrollView.pagingEnabled = YES;
    scrollView.showsHorizontalScrollIndicator = NO;
    scrollView.showsVerticalScrollIndicator = NO;
    scrollView.delegate = self;
    scrollView.bounces = NO;
    self.scrollView = scrollView;
    
    
    UIPageControl *pageController = [[UIPageControl alloc] initWithFrame:CGRectMake(0, [UIScreen mainScreen].bounds.size.height - 40, [UIScreen mainScreen].bounds.size.width, 40)];
    
    self.pageCon = pageController;
    pageController.currentPage = 0;
     pageController.currentPageIndicatorTintColor = [UIColor redColor];
     pageController.pageIndicatorTintColor = [UIColor greenColor];
  
    

    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
   [button setFrame:[GuideLayout getGuideScrollViewFrame]];
    [button addTarget:self action:@selector(removeAnimationDidFinish) forControlEvents:UIControlEventTouchUpInside];
    self.nextButton = button;
    
    
}

- (void)p_showGuideViewWithImages:(NSArray *)images {
    
    if (images.count == 0) {
        
        return;
    }
    self.pageCon.numberOfPages = images.count;
    
    self.images = images;
    
    // 圖片的寬
    CGFloat imageW = self.scrollView.frame.size.width;
    
    for (NSInteger num = 0; num <  images.count; num ++) {
        
        UIImageView *imageView = [[UIImageView alloc] init];
        
        imageView.frame = CGRectMake(num * imageW,0, imageW, self.scrollView.frame.size.height);
        
        imageView.image = [UIImage imageNamed:[images objectAtIndex:num]];
        
        if (num + 1  == images.count) {
            
            imageView.userInteractionEnabled = YES;
            [imageView addSubview:self.nextButton];
            
        }
        
        
        [self.scrollView addSubview:imageView];
        
    }
    
    //設定scrollview的滾動範圍
    CGFloat contentW = images.count *imageW;
    //不允許在垂直方向上進行滾動
    self.scrollView.contentSize = CGSizeMake(contentW, 0);
    
    //.設定分頁
    self.scrollView.pagingEnabled = YES;
    
    
}


- (void)removeGuideComplete:(void(^)(void))aCompleteBlock
{
    self.complete = aCompleteBlock;
    
}

- (void)removeAnimationDidFinish
{
    [self stopScroll];
    [self.scrollView removeFromSuperview];
    [self.pageCon removeFromSuperview];
    
    if (self.complete) {
        self.complete();
    }
    
}

- (void)startScroll
{
    [self stopScroll];
    
    if (!self.timer) {
        self.timer = [NSTimer scheduledTimerWithTimeInterval:kCMBCAdViewsAutoScrollDuration
                                                      target:self
                                                    selector:@selector(p_timerAutoScroll:)
                                                    userInfo:nil
                                                     repeats:YES];
    }
    
    [self.timer setFireDate:[NSDate dateWithTimeIntervalSinceNow:kCMBCAdViewsAutoScrollDuration]];
    
}



-(void)p_timerAutoScroll:(NSTimer *)sender{
    
    int page = (int)self.scrollView.contentOffset.x / self.scrollView.frame.size.width;
    
     page++;
    
   self.pageCon.currentPage = page;
    
    if (page < self.images.count && page > 0) {
        
        [self.scrollView setContentOffset:CGPointMake(page * self.scrollView.frame.size.width, 0) animated:YES];
        
    }else{
        
        if (self.timer.isValid == YES) {
            //計時器暫停
            [self.timer setFireDate:[NSDate distantFuture]];
        }
        
    }
    
    
    
}

- (void)stopScroll
{
    if (self.timer.isValid) {
        [self.timer invalidate];
        self.timer = nil;
    }
}

#pragma mark - UIScrollViewDelegate


- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
    
    NSInteger currentPage = (scrollView.contentOffset.x / SCREEN_WIDTH);
    
    self.pageCon.currentPage = currentPage;
    
    if (currentPage == self.images.count) {
        
        if (self.timer.isValid == YES) {
            //計時器暫停
            [self.timer setFireDate:[NSDate distantFuture]];
        }
    }else{
        if (self.timer.isValid)
        {
            //計時器繼續
            [self.timer setFireDate:[NSDate dateWithTimeIntervalSinceNow:kCMBCAdViewsAutoScrollDuration]];
        }
    }
    
}

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
    
    
    if (self.timer.isValid)
    {
        //計時器繼續
        [self.timer setFireDate:[NSDate dateWithTimeIntervalSinceNow:kCMBCAdViewsAutoScrollDuration]];
    }
    
    
}



- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView {
    
    if (self.timer.isValid == YES) {
        //計時器暫停
        [self.timer setFireDate:[NSDate distantFuture]];
    }
}


#pragma mark - MBCFuncGuideControllerInterface

- (void)loadGuideData{
    
    NSArray *imageList = @[@"one",@"two",@"there",@"fore"];
    
   [self p_showGuideViewWithImages:imageList];
    
}

- (void)showGuideInView:(UIView *)aView{
    
    [self startScroll];
    [aView addSubview:self.scrollView];
    [aView addSubview:self.pageCon];
    
    [[NSUserDefaults standardUserDefaults] setBool:YES forKey:@"First_Guide"];
    
}