YBImageLoop.h
#import
#import
@interface YBImageLoop : NSObject
@property (nonatomic ,strong)NSArray *imageArray; //图片名字数组
@property (nonatomic ,strong)UIPageControl *pageControl;
//构造方法
- (instancetype)initWithScrollViewFrame:(CGRect)rect andContainerView:(UIView *)containerView;
//处理数据
- (void)setImageArray:(NSArray *)imageArray;
//开启定时器
- (void)startTimerWithTime:(NSTimeInterval)time;
@end
YBImageLoop.m
#import "YBImageLoop.h"
@interface YBImageLoop (){
NSTimer *_timer;
}
@property (nonatomic ,strong)UIScrollView *scrollView;
@property (nonatomic ,strong)NSArray *imageNameArray; //图片名字数组
@property (nonatomic ,assign)NSTimeInterval time;
@property (nonatomic ,assign)BOOL isTimer; //判断定时器是否在viewController中创建过
@end
@implementation YBImageLoop
#define SCROLLWIDTH self.scrollView.frame.size.width
#define SCROLLHEIGHT self.scrollView.frame.size.height
- (instancetype)initWithScrollViewFrame:(CGRect)rect andContainerView:(UIView *)containerView
{
self = [super init];
if (self) {
self.scrollView = [[UIScrollView alloc]initWithFrame:rect];
self.scrollView.bounces = NO;
self.scrollView.pagingEnabled = YES;
self.scrollView.showsHorizontalScrollIndicator = NO;
self.scrollView.showsVerticalScrollIndicator = NO;
// sv.contentOffset = CGPointMake(rect.size.width, 0);
[containerView addSubview:self.scrollView];
}
return self;
}
- (void)setImageArray:(NSArray *)imageArray {
_imageArray = imageArray;
self.imageNameArray = imageArray;
self.scrollView.delegate = self;
self.scrollView.contentOffset = CGPointMake(SCROLLWIDTH, 0);
self.scrollView.contentSize = CGSizeMake((imageArray.count+2) * SCROLLWIDTH, 0);
for (NSInteger i = 1; i <= self.imageNameArray.count; i ++) {
UIImageView *iv = [[UIImageView alloc]initWithFrame:CGRectMake(i * SCROLLWIDTH, 0,SCROLLWIDTH , SCROLLHEIGHT)];
iv.image = [UIImage imageNamed:self.imageNameArray[i-1]];
// NSLog(@"%@",self.imageNameArray[i-1]);
[self.scrollView addSubview:iv];
}
UIImageView *firstIV = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, SCROLLWIDTH, SCROLLHEIGHT)];
firstIV.image = [UIImage imageNamed:[self.imageNameArray lastObject]];
[self.scrollView addSubview:firstIV];
UIImageView *lastIV = [[UIImageView alloc]initWithFrame:CGRectMake((self.imageNameArray.count+1)*SCROLLWIDTH, 0, SCROLLWIDTH, SCROLLHEIGHT)];
lastIV.image = [UIImage imageNamed:[self.imageNameArray firstObject]];
[self.scrollView addSubview:lastIV];
BOOL ret = [self.scrollView.delegate respondsToSelector:@selector(scrollViewDidScroll:)];
NSLog(@"%d",ret);
}
- (void)startTimerWithTime:(NSTimeInterval)time {
_timer = [NSTimer scheduledTimerWithTimeInterval:time target:self selector:@selector(timeRun) userInfo:nil repeats:YES];
[[NSRunLoop currentRunLoop]addTimer:_timer forMode:NSRunLoopCommonModes];
self.time = time;
self.isTimer = YES;
}
static int i = 1;
- (void)timeRun {
CGFloat offsetX = self.scrollView.contentOffset.x;
i = offsetX/SCROLLWIDTH;
i++;
if (i == self.imageNameArray.count + 1) {
i = 1;
self.scrollView.contentOffset = CGPointMake(-1, 0);
}
[self.scrollView setContentOffset:CGPointMake(i * SCROLLWIDTH, 0) animated:YES];
}
- (void)removeTimer {
[_timer invalidate];
_timer = nil;
}
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
CGFloat offset_x = scrollView.contentOffset.x;
CGFloat page = offset_x/SCROLLWIDTH;
self.pageControl.currentPage = page -1;
// NSLog(@"page = %f",page);
if (self.imageNameArray.count+1 == page) {
scrollView.contentOffset = CGPointMake(SCROLLWIDTH, 0);
}
else if (page == 0) {
scrollView.contentOffset = CGPointMake(SCROLLWIDTH*self.imageNameArray.count, 0);
}
}
- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView {
[self removeTimer];
}
- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate {
if (self.isTimer == YES) {
[self startTimerWithTime:self.time];
}
}
@end
YBPictureViewController.m
#import "YBPictureViewController.h"
#import "YBImageLoop.h"
@interface YBPictureViewController ()
@property (nonatomic ,strong)YBImageLoop *loop;
@end
@implementation YBPictureViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
self.view.backgroundColor = [UIColor orangeColor];
[self settingImageLoop];
[self settingPageControl];
}
-(void)settingImageLoop {
self.loop = [[YBImageLoop alloc]initWithScrollViewFrame:CGRectMake(20, 100, self.view.width - 40, self.view.width * 400/750) andContainerView:self.view];
self.loop.imageArray = @[@"img_01",@"img_02",@"img_03",@"img_04",@"img_05"];
[self.loop startTimerWithTime:2];
}
-(void)settingPageControl {
UIPageControl *pageControl = [[UIPageControl alloc]initWithFrame:CGRectMake(0, 300, self.view.width, 20)];
pageControl.pageIndicatorTintColor = [UIColor grayColor];
pageControl.currentPageIndicatorTintColor = [UIColor whiteColor];
pageControl.numberOfPages = 5;
[self.view addSubview:pageControl];
self.loop.pageControl = pageControl;
}
@end