YH_GuideViewController.m 3.87 KB
//
//  YH_GuideViewController.m
//  YH_Mall
//
//  Created by YOHO on 15/4/30.
//  Copyright (c) 2015年 YOHO. All rights reserved.
//

#import "YH_GuideViewController.h"
#import "SMPageControl.h"
#define kNumberOfImages 4

@interface YH_GuideViewController ()<UIScrollViewDelegate>

@property (strong ,nonatomic) UIScrollView      *scrollView;
@property (strong ,nonatomic) SMPageControl     *pageControl;
@property (assign ,nonatomic) NSInteger         currentPage;
@property (strong ,nonatomic) UIButton          *enterButton;

@end

@implementation YH_GuideViewController

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

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.enterButton = [UIButton buttonWithType:UIButtonTypeCustom];
    self.enterButton.backgroundColor = [UIColor clearColor];
    [self.enterButton addTarget:self action:@selector(didClickEnterButton:) forControlEvents:UIControlEventTouchUpInside];
}

- (void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
    [self.view addSubview:self.scrollView];
    [self.view insertSubview:self.pageControl aboveSubview:self.scrollView];
}

- (UIScrollView *)scrollView
{
    if(!_scrollView){
        _scrollView = [[UIScrollView alloc] initWithFrame:self.view.bounds];
        [_scrollView setAutoresizingMask:UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight];
        _scrollView.contentSize = CGSizeMake(kScreenWidth * kNumberOfImages, CGRectGetHeight(self.view.frame));
        _scrollView.pagingEnabled = YES;
        _scrollView.delegate = self;
        _scrollView.showsHorizontalScrollIndicator = NO;
        _scrollView.showsVerticalScrollIndicator = NO;
        _scrollView.bounces = NO;
        for (int i = 0; i < kNumberOfImages; i++) {
            UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(i * kScreenWidth , 0, kScreenWidth, kScreenHeight)];
            NSString *imageName = [NSString stringWithFormat:@"guide_%d.png",(i + 1)];
            imageView.image = [UIImage imageNamed:imageName];
            if(i ==  kNumberOfImages - 1){
                imageView.userInteractionEnabled = YES;
                CGSize size = CGSizeMake(124*kScreenPoint6Scale, 45*kScreenPoint6Scale);
                if(Screen35in){
                    self.enterButton.frame = CGRectMake((kScreenWidth - size.width) / 2.0f, 386, size.width, size.height);
                } else{
                    self.enterButton.frame = CGRectMake((kScreenWidth - size.width) / 2.0f, 538*kScreenPoint6Scale, size.width, size.height);
                }
                [imageView addSubview:self.enterButton];
            }
            [self.scrollView addSubview:imageView];
        }
    }
    return _scrollView;
}

- (SMPageControl *)pageControl
{
    if(!_pageControl){
        _pageControl = [[SMPageControl alloc] init];
        _pageControl.indicatorMargin = 18.0f;
        _pageControl.numberOfPages = kNumberOfImages;
        _pageControl.userInteractionEnabled = NO;
        [_pageControl sizeToFit];
        _pageControl.center = self.enterButton.center;
        CGRect frame = _pageControl.frame;
        if(Screen35in){
            frame.origin.y += 0.8 * CGRectGetHeight(self.enterButton.frame);
        } else if(Screen40in){
            frame.origin.y += CGRectGetHeight(self.enterButton.frame);
        } else{
            frame.origin.y += 1.3 * CGRectGetHeight(self.enterButton.frame);
        }
        _pageControl.frame = frame;
    }
    return _pageControl;
}

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    NSInteger page = scrollView.contentOffset.x / kScreenWidth;
    if(page != self.currentPage){
        self.currentPage = page;
    }
    self.pageControl.currentPage = self.currentPage;
}


- (void)didClickEnterButton:(UIButton *)button
{
    if(self.block){
        self.block();
    }
}

- (BOOL)prefersStatusBarHidden
{
    return YES;
}

@end