自定义App首次启动引导页

define BOOLFORKEY @"GuidePage"

@interface GuidePageView ()

@property (nonatomic, strong) UIScrollView *scrollView;
@property (nonatomic, strong) UIPageControl *pageControl;

@end

@implementation GuidePageView

  • (instancetype)initWithFrame:(CGRect)frame
    {
    if (self = [super initWithFrame:frame]) {

      [self show];
    

    }

    return self;
    }

  • (void)show
    {

    if (![[NSUserDefaults standardUserDefaults] boolForKey:BOOLFORKEY]) {

      [[NSUserDefaults standardUserDefaults] setBool:YES forKey:BOOLFORKEY];
      // 自定义引导界面
      [self pushGuideView];
    

    }

}

// 加载视图

  • (void)pushGuideView
    {
    UIWindow *window = [UIApplication sharedApplication].keyWindow;
    self.frame = window.bounds;
    [window addSubview:self];

    _scrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT)];
    [self addSubview:_scrollView];
    _scrollView.backgroundColor = [UIColor whiteColor];
    _scrollView.delegate = self;
    _scrollView.contentSize = CGSizeMake(SCREEN_WIDTH * 4, SCREEN_HEIGHT);
    _scrollView.bounces = NO;
    _scrollView.pagingEnabled = YES;
    _scrollView.showsHorizontalScrollIndicator = NO;

    for (NSInteger i = 0; i < 4; i++) {

      UIImageView *imageView = [[UIImageView alloc]initWithFrame:CGRectMake(SCREEN_WIDTH * i, 0, SCREEN_WIDTH, SCREEN_HEIGHT)];
      NSString *imageName = [NSString stringWithFormat:@"start_%ld",i + 1];
      imageView.image = [UIImage imageNamed:imageName];
      imageView.userInteractionEnabled = YES;
      
      if (i == 3) {
          
          UIButton *startBtn = [UIButton buttonWithType:UIButtonTypeSystem];
          [startBtn setTitle:@"开始体验" forState:UIControlStateNormal];
          startBtn.layer.masksToBounds = YES;
          [startBtn setTitleColor:[UIColor colorWithHex:@"722b04"] forState:UIControlStateNormal];
          startBtn.titleLabel.font = [UIFont systemFontOfSize:19];
          startBtn.layer.cornerRadius = 20;
          startBtn.backgroundColor = [UIColor colorWithHex:kAppDefaultColor_yellow];
          [startBtn addTarget:self action:@selector(buttonClick) forControlEvents:UIControlEventTouchUpInside];
          [imageView addSubview:startBtn];
    
          [startBtn mas_makeConstraints:^(MASConstraintMaker *make) {
              make.bottom.equalTo(imageView.mas_bottom).offset(-50);
              make.centerX.equalTo(imageView.mas_centerX);
              make.width.equalTo(@170);
              make.height.equalTo(@40);
          }];
          
      }
      
      [_scrollView addSubview:imageView];
    

    }

    _pageControl = [[UIPageControl alloc]initWithFrame:CGRectMake(SCREEN_WIDTH * 0.0, SCREEN_HEIGHT * 0.9, SCREEN_WIDTH * 1.0, SCREEN_HEIGHT * 0.1)];
    _pageControl.currentPage = 0;
    _pageControl.numberOfPages = 4;
    _pageControl.pageIndicatorTintColor = [UIColor grayColor];
    _pageControl.currentPageIndicatorTintColor = [UIColor colorWithHex:kAppDefaultColor_yellow];
    [self addSubview:_pageControl];
    }

  • (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollview
    {
    int page = scrollview.contentOffset.x / scrollview.frame.size.width;
    [_pageControl setCurrentPage:page];
    }

  • (void)buttonClick
    {
    [UIView animateWithDuration:1.5 animations:^{

      self.alpha = 0;
    

    } completion:^(BOOL finished) {

      [self removeFromSuperview];
    

    }];
    }

@end

你可能感兴趣的:(自定义App首次启动引导页)