scrollView封装广告轮播实例代码(Hank版)

Posted PJXWang

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了scrollView封装广告轮播实例代码(Hank版)相关的知识,希望对你有一定的参考价值。

#import <UIKit/UIKit.h>

 

@interface PJXPageView : UIView

/* 圆点颜色*/

@property(strong,nonatomic)UIColor *PJX_currentColor;

 

/*没选中的圆点颜色*/

@property(strong,nonatomic)UIColor *PJX_otherColor;

 

/*  图片数据*/

@property(strong,nonatomic)NSArray *PJX_imageNames;

 

+(instancetype)pageView;

@end

 

 

//

//  PJXPageView.m

//  AdShow_PJXWang

//

//  Created by 王俏 on 16/9/26.

//  Copyright © 2016年 wangqiao. All rights reserved.

//

 

#import "PJXPageView.h"

@interface PJXPageView()<UIScrollViewDelegate>

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

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

@property (nonatomic,strong) NSTimer *timer;

 

@end

@implementation PJXPageView

-(instancetype)initWithFrame:(CGRect)frame{

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

        self = [PJXPageView pageView];

    }

    return self;

}

 

-(void)awakeFromNib{

//    NSTimer *timer =  [NSTimer timerWithTimeInterval:1.5 target:self selector:@selector(nextPage:) userInfo:nil repeats:YES];

//    [timer fire];

   self.timer =  [NSTimer scheduledTimerWithTimeInterval:1.5 target:self selector:@selector(nextPage:)  userInfo:nil repeats:YES];

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

}

-(void)nextPage:(NSTimer*)timer

{

    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];

}

-(void)setPJX_imageNames:(NSArray *)pjx_imageNames

{

    _PJX_imageNames = pjx_imageNames;

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

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

        imageView.contentMode = UIViewContentModeScaleAspectFill;

        imageView.clipsToBounds = YES;

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

        [self.scrollView addSubview:imageView];

        

    }

    self.pageControl.numberOfPages = pjx_imageNames.count;

    

}

-(void)setPJX_currentColor:(UIColor *)pjx_currentColor{

    _PJX_currentColor = pjx_currentColor;

     self.pageControl.currentPageIndicatorTintColor = pjx_currentColor;

}

 

-(void)setPJX_otherColor:(UIColor *)pjx_otherColor{

    _PJX_otherColor = pjx_otherColor;

    self.pageControl.pageIndicatorTintColor = pjx_otherColor;

}

 

-(void)layoutSubviews{

    [super layoutSubviews];

    [self.scrollView setContentSize:CGSizeMake(self.scrollView.frame.size.width*self.PJX_imageNames.count, 0)];

    

    for (int i = 0; i < self.scrollView.subviews.count; i++) {

        UIImageView *imageView = self.scrollView.subviews[i];

        [imageView setFrame:CGRectMake(i*self.scrollView.frame.size.width, 0, self.scrollView.frame.size.width, self.scrollView.frame.size.height)];

    }

}

+(instancetype)pageView{

    //实例化自己

    PJXPageView *pageView =[[[NSBundle mainBundle]loadNibNamed:@"PJXPageView" owner:nil options:nil] firstObject];

    [pageView.scrollView setDelegate:pageView];

    return pageView;

}

 

#pragma mark -- UIScrollViewDelegate

-(void)scrollViewDidScroll:(UIScrollView *)scrollView{

    NSUInteger index = (NSUInteger)(scrollView.contentOffset.x/scrollView.frame.size.width+0.5);

    self.pageControl.currentPage = index;

}

 

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

    [self stopTimer];

}

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

     self.timer =  [NSTimer scheduledTimerWithTimeInterval:1.5 target:self selector:@selector(nextPage:)  userInfo:nil repeats:YES];

}

-(void)stopTimer

{

    [self.timer invalidate];

 

}

@end

 

 

使用:

 PJXPageView *page = [[PJXPageView alloc]init];

    page.PJX_imageNames = @[@"001",@"002",@"003",@"004"];

    page.PJX_currentColor = [UIColor blueColor];

    page.PJX_otherColor = [UIColor grayColor];

    page.frame = CGRectMake(100, 100, 220, 220);

    [self.view addSubview:page];

 

以上是关于scrollView封装广告轮播实例代码(Hank版)的主要内容,如果未能解决你的问题,请参考以下文章

iOS: 无限循环轮播图简单封装

猫猫学iOS 之广告轮播图,collectionView制作

iOS广告轮播图

Android无限广告轮播 - 自定义BannerView

IOS开源轮播组件 BannerScrollViewMasonry.h

Android无限广告轮播 - ViewPager源码分析