iOS引导页的快速引用

作者: Lonely_Flyer | 来源:发表于2016-09-23 09:29 被阅读1105次
    • 1.引入MQGuideView文件,根据需要修改引导页数量和图片名称
    static const NSInteger NumberOfGuide = 3; //引导页数
    
     //修改图片名称
    imgView.image = [UIImage imageNamed:[NSString stringWithFormat:@"default_%d",i+1]];
    
    
    • 2.在首页进行登录判断,是否是第一次打开App,并存入本地。初始化MQGuideView,加到当前视图页面,具体在哪里添加随个人喜好,这里只做简单演示。
    - (void)viewWillAppear:(BOOL)animated{
        if (![[NSUserDefaults standardUserDefaults] boolForKey:@"isFirstLaunch"]) {
            _isFirstLaunch = YES;
            [[NSUserDefaults standardUserDefaults] setBool:_isFirstLaunch forKey:@"isFirstLaunch"];
            [[NSUserDefaults standardUserDefaults] synchronize];
            _guideView = [[MQGuideView alloc]initWithFrame:[UIScreen mainScreen].bounds];
            _guideView.delegate = self;
            [self.view addSubview:_guideView];
        }
    }
    
    • 3.添加代理事件,来控制引导页的跳转,这里做了个简单的动画
    - (void)onPassButtonPressed{
        [UIView animateWithDuration:1.0 delay:0 options:UIViewAnimationOptionCurveEaseInOut animations:^{
           _guideView.alpha = 0;
        } completion:^(BOOL finished) {
            [_guideView removeFromSuperview];
        }];
    }
    

    下面是.h和.m文件

    .h文件
    @protocol MQGuideViewDelegate <NSObject>
    //代理方法 跳过引导页
    -(void)onPassButtonPressed;
    @end
    
    
    @interface MQGuideView : UIView
    @property (nonatomic,weak)  id<MQGuideViewDelegate> delegate;
    @end
    
    
    .m文件
    @interface MQGuideView () <UIScrollViewDelegate>
    @property (nonatomic, strong) UIPageControl *pageControl;
    @end
    
    @implementation MQGuideView
    
    static const NSInteger NumberOfGuide = 3; //引导页数
    
    - (instancetype)initWithFrame:(CGRect)frame{
        self = [super initWithFrame:frame];
        if (self) {
            CGFloat screenH = [UIScreen mainScreen].bounds.size.height;
            CGFloat screenW = [UIScreen mainScreen].bounds.size.width;
            UIScrollView *scrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, screenW, screenH)];
            [self addSubview:scrollView];
            
            UIButton *passBtn = [[UIButton alloc]initWithFrame:CGRectMake(screenW-70.0f, 40.0f, 50.0f, 30.0f)];
            [passBtn addTarget:self action:@selector(onFinishedIntroButtonPressed) forControlEvents:UIControlEventTouchUpInside];
            [passBtn setImage:[UIImage imageNamed:@"default_pass"] forState:UIControlStateNormal];
            [self addSubview:passBtn];
            
            scrollView.delegate = self;
            scrollView.backgroundColor = [UIColor whiteColor];
            scrollView.showsHorizontalScrollIndicator = NO;
            scrollView.showsVerticalScrollIndicator = NO;
            scrollView.contentSize = CGSizeMake(screenW*NumberOfGuide, screenH);
            scrollView.pagingEnabled = YES;
            for (int i=0; i<NumberOfGuide; i++) {
                UIImageView *imgView = [[UIImageView alloc]initWithFrame:CGRectMake(i*screenW, 0, screenW, screenH)];
                imgView.contentMode = UIViewContentModeScaleAspectFit;
                imgView.userInteractionEnabled = YES;
                //修改图片名称
                imgView.image = [UIImage imageNamed:[NSString stringWithFormat:@"default_%d",i+1]];
                [scrollView addSubview:imgView];
                
                if (i == (NumberOfGuide-1)) {
                    UIButton *startBtn = [[UIButton alloc]initWithFrame:CGRectMake(self.center.x, self.frame.size.height*.8, 150.0f, 40.0f)];
                    startBtn.center = CGPointMake(self.center.x, self.frame.size.height*.8);
                    startBtn.layer.cornerRadius = 5.0f;
                    startBtn.layer.masksToBounds = YES;
                    startBtn.titleLabel.font = [UIFont systemFontOfSize:17.0f];
                    startBtn.backgroundColor = [UIColor brownColor];
                    [startBtn setTitle:@"立即开启" forState:UIControlStateNormal];
                    [startBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
                    [startBtn addTarget:self action:@selector(onFinishedIntroButtonPressed) forControlEvents:UIControlEventTouchUpInside];
                    [imgView addSubview:startBtn];
                }
            }
            
            _pageControl = [[UIPageControl alloc]initWithFrame:CGRectMake(screenW/2-40.0f, screenH-40.0f, 80.0f, 10.0f)];
            [self addSubview:_pageControl];
            _pageControl.enabled = YES;
            _pageControl.numberOfPages = NumberOfGuide;
            _pageControl.pageIndicatorTintColor = [UIColor lightGrayColor];
            _pageControl.currentPageIndicatorTintColor = [UIColor redColor];
        }
        return  self;
    }
    #pragma mark - 跳过引导代理
    - (void)onFinishedIntroButtonPressed{
        [self.delegate onPassButtonPressed];
    }
    
    #pragma mark - 滑动代理
    - (void)scrollViewDidScroll:(UIScrollView *)scrollView{
        CGFloat scrollViewW = scrollView.width;
        CGFloat x = scrollView.contentOffset.x;
        int page = (x + scrollViewW/2)/scrollViewW;
        _pageControl.currentPage = page;
    }
    @end
    
    

    github链接 https://github.com/zmqFlyer/MQGuideView/tree/master

    相关文章

      网友评论

        本文标题:iOS引导页的快速引用

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