源码-0103-page用到分页的新项目

03-框架设计

消息循环/运行循环

 

#import "ViewController.h"
#import "XMGPageView.h"
#import "XMGLabel.h"

@interface ViewController ()
/** 分页控件 */
@property (nonatomic, strong) XMGPageView *pageView;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    XMGLabel *label = [[XMGLabel alloc] init];
    label.frame = CGRectMake(0, 100, 100, 20);
    label.text = @"原价:998¥";
    [self.view addSubview:label];
    
//    XMGPageView *pageView = [XMGPageView pageView];
//    pageView.frame = CGRectMake(37, 50, 250, 200);
//    pageView.imageNames = @[@"img_00", @"img_01", @"img_02"];
//    pageView.otherColor = [UIColor grayColor];
//    pageView.currentColor = [UIColor orangeColor];
//    [self.view addSubview:pageView];
//    self.pageView = pageView;
}
// 消息循环\运行循环

/**
 * 监听控制器view的点击
 */
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    self.pageView.frame = CGRectMake(0, 0, 200, self.pageView.frame.size.height + 10);
}
@end

 

//
//  XMGPageView.m
//  08-分页

// 框架:
// 1.苹果官方的框架:UIKit
// 2.自定义框架
// 3.第三方框架:流媒体

#import "XMGPageView.h"

@interface XMGPageView() <UIScrollViewDelegate>
@property (weak, nonatomic) IBOutlet UIScrollView *scrollView;
@property (weak, nonatomic) IBOutlet UIPageControl *pageControl;
@end

@implementation XMGPageView
/**
 * 当控件通过代码创建时,就会调用这个方法
 * 当控件通过代码创建时,想做一些初始化操作。应该在这个方法中执行
 */
- (instancetype)initWithFrame:(CGRect)frame
{
    if (self = [super initWithFrame:frame]) {
        [self setup];
        
        // 添加子控件代码
    }
    return self;
}

/**
 * 当控件从xib\storyboard中创建完毕时,就会调用这个方法
 * 当控件从xib\storyboard中创建完毕后的初始化操作。应该在这个方法中执行
 */
- (void)awakeFromNib
{
    [self setup];
}

/**
 * 初始化代码
 */
- (void)setup
{
    self.scrollView.backgroundColor = [UIColor redColor];
}

/**
 * 当控件是通过xib\storyboard创建时,会调用这个方法来初始化控件
 */
- (id)initWithCoder:(NSCoder *)aDecoder
{
    if (self = [super initWithCoder:aDecoder]) {
    }
    return self;
}

+ (instancetype)pageView
{
    return [[[NSBundle mainBundle] loadNibNamed:NSStringFromClass(self) owner:nil options:nil] lastObject];
}

/**
 * 当控件的尺寸发生改变的时候,会自动调用这个方法
 */
- (void)layoutSubviews
{
    [super layoutSubviews];
    
    // 设置scrollView的frame
    self.scrollView.frame = self.bounds;
    
    // 获得scrollview的尺寸
    CGFloat scrollW = self.scrollView.frame.size.width;
    CGFloat scrollH = self.scrollView.frame.size.height;
    
    // 设置pageControl
    CGFloat pageW = 100;
    CGFloat pageH = 20;
    CGFloat pageX = scrollW - pageW;
    CGFloat pageY = scrollH - pageH;
    self.pageControl.frame = CGRectMake(pageX, pageY, pageW, pageH);
    
    // 设置内容大小
    self.scrollView.contentSize = CGSizeMake(self.imageNames.count * scrollW, 0);
    
    // 设置所有imageView的frame
    for (int i = 0; i<self.scrollView.subviews.count; i++) {
        UIImageView *imageView = self.scrollView.subviews[i];
        imageView.frame = CGRectMake(i * scrollW, 0, scrollW, scrollH);
    }
}

#pragma mark - setter方法的重写
- (void)setImageNames:(NSArray *)imageNames
{
    _imageNames = imageNames;
    
    // 移除之前的所有imageView
    // 让subviews数组中的所有对象都执行removeFromSuperview方法
    [self.scrollView.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];
    
    // 根据图片名创建对应个数的imageView
    for (int i = 0; i<imageNames.count; i++) {
        UIImageView *imageView = [[UIImageView alloc] init];
        imageView.image = [UIImage imageNamed:imageNames[i]];
        [self.scrollView addSubview:imageView];
    }
    
    // 设置总页数
    self.pageControl.numberOfPages = imageNames.count;
//    if (imageNames.count <= 1) {
//        self.pageControl.hidden = YES;
//    } else {
//        self.pageControl.hidden = NO;
//    }
//    self.pageControl.hidden = imageNames.count <= 1;
//    self.pageControl.hidesForSinglePage = YES;
}

- (void)setCurrentColor:(UIColor *)currentColor
{
    _currentColor = currentColor;
    
    self.pageControl.currentPageIndicatorTintColor = currentColor;
}

- (void)setOtherColor:(UIColor *)otherColor
{
    _otherColor = otherColor;
    
    self.pageControl.pageIndicatorTintColor = otherColor;
}

#pragma mark - <UIScrollViewDelegate>
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    self.pageControl.currentPage = (int)(scrollView.contentOffset.x / scrollView.frame.size.width + 0.5);
}
@end

 添加系统自带的一些框架;

比如map

 

定时器

//
//  ViewController.m
//  08-分页
#import "ViewController.h"
#import "XMGPageView.h"
#import "XMGLabel.h"

@interface ViewController ()
/** 分页控件 */
@property (nonatomic, strong) XMGPageView *pageView;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
//    XMGLabel *label = [[XMGLabel alloc] init];
//    label.frame = CGRectMake(0, 100, 100, 20);
//    label.text = @"原价:998¥";
//    [self.view addSubview:label];
    
    XMGPageView *pageView = [XMGPageView pageView];
    pageView.frame = CGRectMake(37, 50, 300, 130);
    pageView.imageNames = @[@"img_00", @"img_01", @"img_02", @"img_03", @"img_04"];
    pageView.otherColor = [UIColor grayColor];
    pageView.currentColor = [UIColor orangeColor];
    [self.view addSubview:pageView];
    self.pageView = pageView;
}
// 消息循环\运行循环

/**
 * 监听控制器view的点击
 */
//- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
//{
//    self.pageView.frame = CGRectMake(0, 0, 200, self.pageView.frame.size.height + 10);
//}
@end

 

//
//  XMGPageView.m
// 框架:
// 1.苹果官方的框架:UIKit
// 2.自定义框架
// 3.第三方框架:流媒体

#import "XMGPageView.h"

@interface XMGPageView() <UIScrollViewDelegate>
@property (weak, nonatomic) IBOutlet UIScrollView *scrollView;
@property (weak, nonatomic) IBOutlet UIPageControl *pageControl;
/** 定时器 */
@property (nonatomic, strong) NSTimer *timer;
@end

@implementation XMGPageView
#pragma mark - 初始化方法
/**
 * 当控件通过代码创建时,就会调用这个方法
 * 当控件通过代码创建时,想做一些初始化操作。应该在这个方法中执行
 */
- (instancetype)initWithFrame:(CGRect)frame
{
    if (self = [super initWithFrame:frame]) {
        [self setup];
        
        // 添加子控件代码
    }
    return self;
}

/**
 * 当控件从xib\storyboard中创建完毕时,就会调用这个方法
 * 当控件从xib\storyboard中创建完毕后的初始化操作。应该在这个方法中执行
 */
- (void)awakeFromNib
{
    [self setup];
}

/**
 * 初始化代码
 */
- (void)setup
{
    self.scrollView.backgroundColor = [UIColor redColor];
    
    // 开启定时器
    [self startTimer];
}

+ (instancetype)pageView
{
    return [[[NSBundle mainBundle] loadNibNamed:NSStringFromClass(self) owner:nil options:nil] lastObject];
}

/**
 * 当控件的尺寸发生改变的时候,会自动调用这个方法
 */
- (void)layoutSubviews
{
    [super layoutSubviews];
    
    // 设置scrollView的frame
    self.scrollView.frame = self.bounds;
    
    // 获得scrollview的尺寸
    CGFloat scrollW = self.scrollView.frame.size.width;
    CGFloat scrollH = self.scrollView.frame.size.height;
    
    // 设置pageControl
    CGFloat pageW = 100;
    CGFloat pageH = 20;
    CGFloat pageX = scrollW - pageW;
    CGFloat pageY = scrollH - pageH;
    self.pageControl.frame = CGRectMake(pageX, pageY, pageW, pageH);
    
    // 设置内容大小
    self.scrollView.contentSize = CGSizeMake(self.imageNames.count * scrollW, 0);
    
    // 设置所有imageView的frame
    for (int i = 0; i<self.scrollView.subviews.count; i++) {
        UIImageView *imageView = self.scrollView.subviews[i];
        imageView.frame = CGRectMake(i * scrollW, 0, scrollW, scrollH);
    }
}

#pragma mark - setter方法的重写
- (void)setImageNames:(NSArray *)imageNames
{
    _imageNames = imageNames;
    
    // 移除之前的所有imageView
    // 让subviews数组中的所有对象都执行removeFromSuperview方法
    [self.scrollView.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];
    
    // 根据图片名创建对应个数的imageView
    for (int i = 0; i<imageNames.count; i++) {
        UIImageView *imageView = [[UIImageView alloc] init];
        imageView.image = [UIImage imageNamed:imageNames[i]];
        [self.scrollView addSubview:imageView];
    }
    
    // 设置总页数
    self.pageControl.numberOfPages = imageNames.count;
//    if (imageNames.count <= 1) {
//        self.pageControl.hidden = YES;
//    } else {
//        self.pageControl.hidden = NO;
//    }
//    self.pageControl.hidden = imageNames.count <= 1;
//    self.pageControl.hidesForSinglePage = YES;
}

- (void)setCurrentColor:(UIColor *)currentColor
{
    _currentColor = currentColor;
    
    self.pageControl.currentPageIndicatorTintColor = currentColor;
}

- (void)setOtherColor:(UIColor *)otherColor
{
    _otherColor = otherColor;
    
    self.pageControl.pageIndicatorTintColor = otherColor;
}

#pragma mark - <UIScrollViewDelegate>
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    self.pageControl.currentPage = (int)(scrollView.contentOffset.x / scrollView.frame.size.width + 0.5);
}

- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView
{
    [self stopTimer];
}

- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate
{
    [self startTimer];
}

#pragma mark - 定时器控制
- (void)startTimer
{
    // 创建一个定时器
    self.timer = [NSTimer scheduledTimerWithTimeInterval:1.5 target:self selector:@selector(nextPage) userInfo:nil repeats:YES];
    [[NSRunLoop mainRunLoop] addTimer:self.timer forMode:NSRunLoopCommonModes];
}

- (void)stopTimer
{
    [self.timer invalidate];
    self.timer = nil;
}

/**
 * 下一页
 */
- (void)nextPage
{
    NSInteger page = self.pageControl.currentPage + 1;
    if (page == self.pageControl.numberOfPages) {
        page = 0;
    }
    
    CGPoint offset = self.scrollView.contentOffset;
    offset.x = page * self.scrollView.frame.size.width;
    [self.scrollView setContentOffset:offset animated:YES];   // animate at constant velocity to new offset
    NSLog(@"nextPage");
}
@end

 

posted @ 2017-02-18 14:49  laugh  阅读(238)  评论(0编辑  收藏  举报