码迷,mamicode.com
首页 > 其他好文 > 详细

封装分页功能

时间:2016-05-02 22:59:11      阅读:123      评论:0      收藏:0      [点我收藏+]

标签:

#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

封装分页功能

标签:

原文地址:http://www.cnblogs.com/liuzhenjie/p/5453337.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!