• 封装分页功能


    #import <UIKit/UIKit.h>

     

    @interface LZJPageView : UIView

     

    /** 图片名数据 */

    @property (nonatomic, strong) NSArray *imageNames;

    + (instancetype)pageView;

    @end

     


     

     

     

     

    #import "LZJPageView.h"

     

    @interface LZJPageView () <UIScrollViewDelegate>

    @property (weak, nonatomic) IBOutlet UIScrollView *scrollView;

    @property (weak, nonatomic) IBOutlet UIPageControl *pageControl;

     

     

    /** 定时器 */

    @property (nonatomic, weak) NSTimer *timer;

     

    @end

    @implementation LZJPageView

     

    - (void)awakeFromNib

    {

        // 1.单页的时候是否隐藏pageControl

        self.pageControl.hidesForSinglePage = YES;

        

        // 2.设置pageControl的图片

        [self.pageControl setValue:[UIImage imageNamed:@"current"] forKeyPath:@"_currentPageImage"];

        [self.pageControl setValue:[UIImage imageNamed:@"other"]  forKeyPath: @"_pageImage"];

        

        // 3.开启定时器

        [self startTimer];

     

    }

     

    + (instancetype)pageView

    {

        return [[[NSBundle mainBundle] loadNibNamed:NSStringFromClass(self) owner:nil options:nil]  lastObject];

    }

     

    - (void)setImageNames:(NSArray *)imageNames

    {

        _imageNames = imageNames;

        // 0.移除之前添加的

        // subviews这个数组中每一个对象都执行removeFromSuperview

        [self.scrollView.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];

        

        // 1.根据图片名数据创建ImageView添加到scrollView

        CGFloat scrollViewW = self.scrollView.frame.size.width;

        CGFloat scrollViewH = self.scrollView.frame.size.height;

        NSUInteger count = imageNames.count;

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

            UIImageView *imageView = [[UIImageView alloc] init];

            imageView.image = [UIImage imageNamed:imageNames[i]];

            imageView.frame = CGRectMake(i * scrollViewW, 0, scrollViewW, scrollViewH);

            [self.scrollView addSubview:imageView];

        }

        

        // 2.设置contentSize

        self.scrollView.contentSize = CGSizeMake(count * scrollViewW, 0);

        

        // 3.设置总页数

        self.pageControl.numberOfPages = count;

    }

     

    #pragma mark - 定时器相关的代码

    - (void)startTimer

    {

        // 返回一个自动执行的定时器对象

        self.timer = [NSTimer scheduledTimerWithTimeInterval:2.0 target:self selector:@selector(nextPage:) userInfo:@"123" repeats:YES];

        

        [[NSRunLoop mainRunLoop] addTimer:self.timer forMode:NSRunLoopCommonModes];

    }

     

    - (void)stopTimer

    {

        [self.timer invalidate];

    }

     

    /**

     *  滚动到下一页

     */

    - (void)nextPage:(NSTimer *)timer

    {

        // 1.计算下一页的页码

        NSInteger page = self.pageControl.currentPage + 1;

        

        // 2.超过了最后一页

        if ( page == self.imageNames.count) {

            page = 0;

        }

        

        // 3.滚动到下一页

        [self.scrollView setContentOffset:CGPointMake(page * self.scrollView.frame.size.width, 0) animated:YES];

    }

     

    #pragma mark - UIScrollViewDelegate

     

    - (void)scrollViewDidScroll:(UIScrollView *)scrollView

    {

        // 1.计算页码

        int page = (int)(scrollView.contentOffset.x / scrollView.frame.size.width + 0.5);

        

        // 2.设置页码

        self.pageControl.currentPage = page;

    }

     

    /**

     *  用户即将开始拖拽scrollView,停止定时器

     */

    - (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView

    {

        [self stopTimer];

    }

     

    /**

     *  用户已经停止拖拽scrollView,开启定时器

     */

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

    {

        [self startTimer];

    }

     

    @end

  • 相关阅读:
    漫谈二分查找Binary Search (转)
    怎样搭建Android开发平台(转)
    vim控,键盘控的福利,在chrome和firefox中用vim
    用dos命令行执行java程序的方法
    简明 Vim 练级攻略(转)
    vim中文手册,gcc中文手册,gdb中文手册打包赠送
    使用Eclipse平台开发Java程序(转)
    使用Eclipse创建一个Android程序方法
    hdu 1024 Max Sum Plus Plus (最大m子段和)(经典DP)(转)
    sql server2008 表分区
  • 原文地址:https://www.cnblogs.com/liuzhenjie/p/5453337.html
Copyright © 2020-2023  润新知