美文网首页
6.4 引导页的封装

6.4 引导页的封装

作者: 草根小强 | 来源:发表于2019-04-17 11:47 被阅读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

相关文章

  • 6.4 引导页的封装

    引导页的封装1.png 引导页的封装2.png 引导页的封装3.png

  • 引导页的封装

    一、写作原因 以前都没有想着来写点东西,今天遇到件事情让我决定每次还是要做记录。因为以前自己可以轻松的完成p...

  • iOS引导页封装

    插眼传送 引导页是App必不可少的功能之一;本Demo用简单有效的代码,封装引导页,仅需3行代码搞定,同时支持GI...

  • 关于iOS swift3.0 UICollectionView封

    关于swift封装的轮播图和引导页请看链接blog.csdn.net/cheniOSjourney/article...

  • XAnimLayout,让引导页动画easy

    XAnimLayout封装了常见的引导页动画,仅仅需要在xml中设置相关属性,即可实现translate、rota...

  • 一句话搞定新版本引导页(iOS开发)

    目前基本上所有APP均有新版本引导页,所以封装一个使用简便的就显得尤为重要了。(个人推荐:一句话搞定新版本引导页 ...

  • (Swift4.0)App引导页封装

    1.自定义引导页控制器 import UIKit let kScreenWidth = UIScreen.main...

  • APP 引导页设计5-总结

    前面4篇分别介绍了引导页的分类、展现方式、优秀引导页的特点、如何设计优秀的引导页,是时候来个总结了。 引导页分类:...

  • iOS最好用的引导页

    最近项目结束的时候又要改引导页,之前写的启动页改起来太麻烦了,所以就直接封装一个,功能可能还不是很完善,但是感觉用...

  • 引导页

    // // ViewController.m // 引导页_课堂练习 // // Created by 张羽婷 o...

网友评论

      本文标题:6.4 引导页的封装

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