6.4 引导页的封装

2019-04-17  本文已影响0人  草根小强
#import "ViewController.h"
#import "IntroduceView.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
//    self.view.backgroundColor = [UIColor redColor];
    
    
    // 添加引导页
    NSArray *array = @[@"0",@"1",@"2"];
    CGRect frame = self.view.frame;
    IntroduceView *introduceView = [[IntroduceView alloc] initWithImageNameOfArray:array frame:frame];
self.view.backgroundColor = [UIColor yellowColor];
    [self.view addSubview:introduceView];
}


- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    NSLog(@"我是可以点得 你冤枉我了");
}
@end

#import <UIKit/UIKit.h>

@interface IntroduceView : UIView
- (instancetype)initWithImageNameOfArray:(NSArray *)array frame:(CGRect )frame;
@end
#define VIEW_W self.frame.size.width

#import "IntroduceView.h"

@interface IntroduceView ()<UIScrollViewDelegate>
{
    NSArray *_arrayOfImageName;
    UIScrollView *_scrollView;
    UIPageControl *_pageControl;
}
@end

@implementation IntroduceView

- (instancetype)initWithImageNameOfArray:(NSArray *)array frame:(CGRect)frame
{
    if (self = [super initWithFrame:frame]) {
        
        // 0.设置大小
        self.frame = frame;
        
        // 1. 获取图片信息
        _arrayOfImageName = array;
        
        // 2. 添加显示图片的scrollview
        [self addScrollView];
        
        // 3. 添加分页控制器
        [self addPageControl];
        
        // 4. 添加进入app按钮
        [self addEnterButton];
        
    }
    return self;
}

- (void)addEnterButton
{
    CGFloat btnX = (_arrayOfImageName.count - 1) * _scrollView.frame.size.width;
    CGFloat btnW = _scrollView.frame.size.width;
    CGFloat btnH = 60;
    CGFloat btnY = _scrollView.frame.size.height - 120;
    UIButton *btn = [[UIButton alloc] initWithFrame:CGRectMake(btnX, btnY, btnW, btnH)];
    [btn setTitle:@"WELCOME TO APP" forState:UIControlStateNormal];
    [btn setTitleColor:[UIColor yellowColor] forState:UIControlStateNormal];
    btn.titleLabel.font = [UIFont systemFontOfSize:30];
    [btn addTarget:self  action:@selector(clickButton) forControlEvents:UIControlEventTouchUpInside];
    [_scrollView addSubview:btn];
}

- (void)clickButton
{
    [UIView animateWithDuration:2.0 animations:^{
        self.alpha = 0;
        self.transform = CGAffineTransformScale(self.transform, 0.2, 0.2);
    } completion:^(BOOL finished) {
        [self removeFromSuperview];
    }];
}

- (void)addPageControl
{
    CGFloat pageControlX = 0;
    CGFloat pageControlW = VIEW_W;
    CGFloat pageControlH = 60;
    CGFloat pageControlY = self.frame.size.height - pageControlH;
    _pageControl = [[UIPageControl alloc] initWithFrame:CGRectMake(pageControlX, pageControlY, pageControlW, pageControlH)];
    _pageControl.numberOfPages = _arrayOfImageName.count;
    _pageControl.userInteractionEnabled = NO;
    [self addSubview:_pageControl];
}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    CGFloat offSetX = scrollView.contentOffset.x;
    NSInteger pageNum = offSetX / _scrollView.frame.size.width;
    _pageControl.currentPage = pageNum;
}


- (void)addScrollView
{
    // 1. 获取一个滚动视图
    _scrollView = [[UIScrollView alloc] initWithFrame:self.frame];
    _scrollView.contentSize = CGSizeMake(VIEW_W * _arrayOfImageName.count, 0);
    _scrollView.pagingEnabled = YES;
    _scrollView.showsHorizontalScrollIndicator = NO;
    _scrollView.bounces = NO;
    _scrollView.delegate = self;
    
    // 2. 添加展示图片
    for (int i = 0; i < _arrayOfImageName.count; i ++) {
        UIImage *image = [UIImage imageNamed:_arrayOfImageName[i]];
        CGFloat imageViewX = VIEW_W * i;
        UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(imageViewX, 0, VIEW_W, self.frame.size.height)];
        imageView.image = image;
        [_scrollView addSubview:imageView];
    }
    
    // 3. 把滚动视图添加到View
    [self addSubview:_scrollView];
}

@end
引导页的封装1.png
引导页的封装2.png
引导页的封装3.png
上一篇 下一篇

猜你喜欢

热点阅读