iOS中的图片轮播器

来源:互联网 发布:知圆链条索具 编辑:程序博客网 时间:2024/06/07 19:53
  1. //TableViewController .m中

  2. //  TestTableViewController.m

  3. //  图片轮播器

  4. //

  5. //  Created by 张德华 on 5/17/16.

  6. //  Copyright © 2016 张德华. All rights reserved.

  7. //

  8. #import "TestTableViewController.h"

  9. #import "ScrollModel.h"

  10. #import "ScrollView.h"

  11. @interface TestTableViewController ()

  12. @property (nonatomic,strong)NSArray *dataArray;

  13. @end

  14. @implementation TestTableViewController

  15. #pragma mark - 懒加载

  16. - (NSArray *)dataArray{

  17.    if(nil == _dataArray){

  18.        //获取路径

  19.        NSString *path = [[NSBundle mainBundle]pathForResource:@"ad.plist" ofType:nil];

  20.        //读取

  21.        NSArray *tempArray = [NSArray arrayWithContentsOfFile:path];

  22.        //创建可变数组

  23.        NSMutableArray *mutable = [NSMutableArray array];

  24.        //遍历

  25.        for (NSDictionary *dict in tempArray) {

  26.            ScrollModel *model = [ScrollModel scrollModelWithDict:dict];

  27.            //数据模型 存入数组

  28.            [mutable addObject:model];

  29.        }

  30.        _dataArray = mutable;

  31.    }

  32.    return _dataArray;

  33. }

  34. - (void)viewDidLoad {

  35.    [super viewDidLoad];

  36.  

  37.    //实例化滚动视图

  38.    ScrollView *scrollView = [[ScrollView alloc]initWithFrame:CGRectMake(0, 0, 375, 260)];

  39.    

  40.    //传递数据

  41.    scrollView.dataArray = self.dataArray;

  42.    

  43.    //添加到屏幕

  44.    self.tableView.tableHeaderView = scrollView;

  45.    

  46. }

  47. @end

  1. //数据模型.h中

  2. //  ScrollModel.h

  3. //  图片轮播器

  4. //

  5. //  Created by 张德华 on 5/17/16.

  6. //  Copyright © 2016 张德华. All rights reserved.

  7. //

  8. #import <Foundation/Foundation.h>

  9. @interface ScrollModel : NSObject

  10. @property (nonatomic,copy) NSString *imgsrc;

  11. @property (nonatomic,copy) NSString *title;

  12. //对象方法

  13. - (instancetype)initWithDict:(NSDictionary *)dict;

  14. //类方法

  15. + (instancetype)scrollModelWithDict:(NSDictionary *)dict;

  16. @end


  1. //数据模型 .m中

  2. //  ScrollModel.m

  3. //  图片轮播器

  4. //

  5. //  Created by 张德华 on 5/17/16.

  6. //  Copyright © 2016 张德华. All rights reserved.

  7. //

  8. #import "ScrollModel.h"

  9. @implementation ScrollModel

  10. //对象方法

  11. - (instancetype)initWithDict:(NSDictionary *)dict{

  12.    if (self = [super init]) {

  13.        [self setValuesForKeysWithDictionary:dict];

  14.    }

  15.    return self;

  16. }

  17. //类方法

  18. + (instancetype)scrollModelWithDict:(NSDictionary *)dict{

  19.    return [[self alloc]initWithDict:dict];

  20. }

  21. @end


  1. //自定义View .h中

  2. //  ScrollView.h

  3. //  图片轮播器

  4. //

  5. //  Created by 张德华 on 5/17/16.

  6. //  Copyright © 2016 张德华. All rights reserved.

  7. //

  8. #import <UIKit/UIKit.h>

  9. @interface ScrollView : UIView

  10. //接收外部传递的数据

  11. @property (nonatomic,strong) NSArray *dataArray;

  12. @end


  1. //自定义View .m中

  2. //  ScrollView.m

  3. //  图片轮播器

  4. //

  5. //  Created by 张德华 on 5/17/16.

  6. //  Copyright © 2016 张德华. All rights reserved.

  7. //

  8. #import "ScrollView.h"

  9. #import "ScrollModel.h"

  10. #define kViewSize ([UIScreen mainScreen].bounds.size)

  11. #define kScrollSize (_scrollView.frame.size)

  12. @interface ScrollView () <UIScrollViewDelegate>

  13. //滚动视图

  14. @property (nonatomic,weak) UIScrollView *scrollView;

  15. //分页控制器

  16. @property (nonatomic,weak) UIPageControl *pageControl;

  17. //显示文本

  18. @property (nonatomic,weak) UILabel *titleLabel;

  19. //定时器

  20. @property (nonatomic,weak) NSTimer *timer;

  21. @end

  22. @implementation ScrollView

  23. #pragma mark - 初始化界面

  24. - (instancetype)initWithFrame:(CGRect)frame{

  25.    if (self = [super initWithFrame:frame]){

  26.        

  27.        //初始化scrollView

  28.        [self initScrollView];

  29.        

  30.        //初始化pageControl

  31.        [self initPageControl];

  32.        

  33.        //初始化titleLabel

  34.        [self initTitleLabel];

  35.        

  36.        //添加定时器

  37.        [self setupTimer];

  38.        

  39.    }

  40.    return self;

  41. }

  42. #pragma mark - 初始化scrollView

  43. - (void)initScrollView{

  44.    

  45.    //实例化scrollView

  46.    UIScrollView *scrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(0,0, kViewSize.width, 260)];

  47.    

  48.    //关联属性

  49.    self.scrollView = scrollView;

  50.    

  51.    //添加到当前view

  52.    [self addSubview:scrollView];

  53.    

  54.    //设置代理

  55.    scrollView.delegate = self;

  56.    

  57. }

  58. #pragma mark - 初始化pageControl

  59. - (void)initPageControl{

  60.    

  61.    //实例化pageControl

  62.    UIPageControl *pageControl = [[UIPageControl alloc]initWithFrame:CGRectMake(kScrollSize.width/3*2+5, kScrollSize.height-30, kScrollSize.width/3-10, 20)];

  63.    

  64.    //关联属性

  65.    self.pageControl = pageControl;

  66.    

  67.    //添加到当前view

  68.    [self addSubview:pageControl];

  69.    //当前页颜色

  70.    pageControl.currentPageIndicatorTintColor = [UIColor yellowColor];

  71.    

  72.    //非当前页颜色

  73.    pageControl.pageIndicatorTintColor = [UIColor grayColor];

  74.    

  75.    //当前页码

  76.    pageControl.currentPage = 0;

  77.    

  78. }

  79. #pragma mark - 初始化titleLabel

  80. - (void)initTitleLabel{

  81.    

  82.    //实例化titleLabel

  83.    UILabel *titleLabel = [[UILabel alloc]initWithFrame:CGRectMake(5, kScrollSize.height-30, kScrollSize.width/3*2, 20)];

  84.    

  85.    //关联属性

  86.    self.titleLabel = titleLabel;

  87.    

  88.    //添加到当前view

  89.    [self addSubview:titleLabel];

  90.    

  91.    //设置文本颜色

  92.    titleLabel.textColor = [UIColor whiteColor];

  93.    

  94.    //设置字体大小

  95.    titleLabel.font = [UIFont systemFontOfSize:14];

  96. }

  97. #pragma mark - 添加定时器

  98. - (void)setupTimer{

  99.    

  100.    NSTimer *timer = [NSTimer scheduledTimerWithTimeInterval:2

  101.                                                      target:self

  102.                                                    selector:@selector(changeData)

  103.                                                    userInfo:nil

  104.                                                     repeats:YES];

  105.    self.timer = timer;

  106.    NSRunLoop *runLoop = [NSRunLoop currentRunLoop];

  107.    [runLoop addTimer:timer forMode:NSRunLoopCommonModes];

  108. }

  109. //定时器调用方法

  110. - (void)changeData{

  111.    

  112.    //得到当前页码

  113.    NSInteger currentPage = _pageControl.currentPage;

  114.    

  115.    //判断

  116.    if (currentPage == _dataArray.count-1){

  117.        //图片显示到最后一张,跳回第一张

  118.        currentPage = 0;

  119.    } else {

  120.        //图片没有显示到最后一张,跳到下一张

  121.        currentPage++;

  122.    }

  123.    

  124.    //页码传递回去,改变页码显示

  125.    _pageControl.currentPage = currentPage;

  126.    

  127.    //显示页码对应的图片 (带有动画效果)

  128.    [_scrollView setContentOffset:CGPointMake(kScrollSize.width * currentPage, 0) animated:YES];

  129.    

  130.    //显示页码对应的文本内容

  131.    ScrollModel *model = _dataArray[currentPage];

  132.    _titleLabel.text = model.title;

  133.    

  134. }

  135. #warning 只有拿到数据才能进行设置,否则只能做初始化操作

  136. #pragma mark - 重写数组属性set方法 接收外部传进来的数据 并在这里对界面进行设置 (拿到的是存了模型的数组)

  137. - (void)setDataArray:(NSArray *)dataArray{

  138.    

  139.    _dataArray = dataArray;

  140.    

  141.    //设置scrollView界面

  142.    [self setupScroll];

  143.    

  144.    //设置分页

  145.    [self setupPageControl];

  146.    

  147.    //设置初始显示文本

  148.    [self setupInitTitle];

  149. }

  150. #pragma mark - 设置滚动视图

  151. - (void)setupScroll{

  152.    

  153.    

  154.    for(int i = 0; i < _dataArray.count; i++){

  155.        

  156.        //拿到数据模型

  157.        ScrollModel *model = _dataArray[i];

  158.        

  159.        //拼接图片名称

  160.        NSString *imageString = [NSString stringWithFormat:@"%@",model.imgsrc];

  161.        

  162.        //设置x

  163.        CGFloat imageX = kScrollSize.width * i;

  164.        

  165.        //实例化imageView

  166.        UIImageView *imageView = [[UIImageView alloc]initWithFrame:CGRectMake(imageX, 0, kScrollSize.width, kScrollSize.height)];

  167.        

  168.        //实例化image

  169.        UIImage *image = [UIImage imageNamed:imageString];

  170.        

  171.        //添加图片到imageView

  172.        imageView.image = image;

  173.        

  174.        //添加到scrollView

  175.        [_scrollView addSubview:imageView];

  176.        

  177.    }

  178.    

  179.    //打开分页效果

  180.    _scrollView.pagingEnabled = YES;

  181.    

  182.    //滚动范围

  183.    [_scrollView setContentSize:CGSizeMake(kScrollSize.width * _dataArray.count,0)];

  184.    

  185. }

  186. #pragma mark - 设置分页控制器

  187. - (void)setupPageControl{

  188.    

  189.    //总页数

  190.    _pageControl.numberOfPages = _dataArray.count;

  191.    

  192. }

  193. #pragma mark - 设置初始显示文本

  194. - (void)setupInitTitle{

  195.    

  196.    ScrollModel *model = _dataArray[0];

  197.    _titleLabel.text = model.title;

  198.    

  199. }

  200. #pragma mark - 代理方法 --> 停止滚动后调用

  201. - (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{

  202.    

  203.    //设置当前图片对应的页码

  204.    _pageControl.currentPage = _scrollView.contentOffset.x / kScrollSize.width;

  205.    

  206.    //设置显示文本

  207.    ScrollModel *model = self.dataArray[_pageControl.currentPage];

  208.    _titleLabel.text = model.title;

  209.    

  210. }

  211. #pragma mark - 代理方法 --> 开始拖拽时调用 (手触摸的那一刻)

  212. - (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView{

  213.    

  214.    //关闭定时器

  215.    [_timer invalidate];

  216.    

  217. }

  218. #pragma mark - 代理方法 --> 停止拖拽时调用 (手离开的那一刻)

  219. - (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate{

  220.    

  221.    //开启定时器

  222.    [self setupTimer];

  223.    

  224.    

  225. }

  226. /**一定要释放*/

    - (void)dealloc{

        [_timer invalidate];

        _timer = nil;

    }

  227. @end

实现步骤:

1.创建ScrollView : UIView (这个ScrollView类相当于一个Viwe视图,可以在上面添加控件)

2.在控制器中导入ScrollView类,并在ViewDidLoad中实例化,添加到当前控制器的View上

3.创建数据模型类ScrollModel : NSObject (对轮播器上要显示的数据进行懒加载)

4.在控制器中导入ScrollModel类,并实现懒加载方法,将数据模型保存到数组中

5.在ScrollView类中实现在控制器中创建的ScrollView对象的初始化方法 -(initWithFrame:)

6.在初始化方法中,需要初始化的有:滚动视图,分页控制器,初始显示文本

7.在ScrollView.h中定义数组,用来接收控制器传递过来的存了模型的数组数据

8.在控制器中给ScrollView对象的数组属性赋值 (赋值后ScrollView类中通过重写数组属性的set方法就接收到了数据)

9.在ScrollView.m中重写数组属性的set方法,接收传递进来的数据,并赋值给自己(数组属性)

10.ScrollView类中有了数据后,就可以对界面上的控件进行设置

    1.设置滚动视图的图片 (拼接图片添加到scrollView)

    2.设置pageControl (初始化的时候没有数据,不能设置总"点"数,只能在拿到数据后在这里设置)

    3.设置初始显示文本 (初始化的时候没有数据,所以不能设置初始显示文本,要在这里拿到数据后再进行设置)

11.设置定时器,让滚动视图自动滚动

12.设置完成后调用代理方法,监听用户的操作,做出相应操作(开始拖拽:关闭定时器,停止拖拽:打开定时器,图片停止滚动:设置页码和显示文本)


ad.plist文件内容


























0 0
原创粉丝点击