定时器+分页

定时器+分页


#import 

@interface XMGPageView : UIView
+ (instancetype)pageView;
/** 图片名字 */
@property (nonatomic, strong) NSArray *imageNames;
/** 其他圆点颜色 */
@property (nonatomic, strong) UIColor *otherColor;
/** 当前圆点颜色 */
@property (nonatomic, strong) UIColor *currentColor;


@end




#import "XMGPageView.h"

@interface XMGPageView() 
@property (weak, nonatomic) IBOutlet UIScrollView *scrollView;
@property (weak, nonatomic) IBOutlet UIPageControl *pageControl;
/**  定时器  */
@property(nonatomic,strong)NSTimer * timer ;
@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
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    self.pageControl.currentPage = (int)(scrollView.contentOffset.x / scrollView.frame.size.width + 0.5);
}


#pragma mark -定时器设置

-(void)scrollViewWillBeginDragging:(UIScrollView *)scrollView
{
   [self stopTimer];
}
-(void)stopTimer
{
    [self.timer invalidate];
    self.timer=nil;
}

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

-(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)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];
    NSLog(@"---------nextPage");
}



@end



    
    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;




你可能感兴趣的:(定时器+分页)