ios新手引导页(oc 和swift)

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/Z1591090/article/details/83311254

在第一次安装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"];
    
}

没有更多推荐了,返回首页