在现在的很多项目经常会用到轮播图,下面是我自己写的一个轮播图的实现
在RootView.h中
@interface RootView : UIView
@property(nonatomic , strong)UIScrollView *scrollV;
@property(nonatomic , strong)UIPageControl *pageC;
@end
RootView.m中
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
// Initialization code
self.backgroundColor = [UIColor cyanColor];
[self addAllViews];
}
return self;
}
- (void)addAllViews
{
self.scrollV = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 200, [UIScreen mainScreen].bounds.size.width, 200)];
self.scrollV.backgroundColor = [UIColor whiteColor];
//不显示水平方向的滚动条
self.scrollV.showsHorizontalScrollIndicator = NO;
//设置可以分页滚动
self.scrollV.pagingEnabled = YES;
//关闭边界的弹动效果
self.scrollV.bounces = NO;
[self addSubview:self.scrollV];
self.pageC = [[UIPageControl alloc] initWithFrame:CGRectMake(10, 370,100, 20)];
self.pageC.numberOfPages = 6;
self.pageC.userInteractionEnabled = NO;
[self addSubview:self.pageC];
}
@end
在控制器中RootViewController.m
#import "RootViewController.h"
#import "RootView.h"
@interface RootViewController ()<UIScrollViewDelegate>
@property(nonatomic , strong)RootView *rv;
@property(nonatomic , strong)NSTimer *timer;
@end
@implementation RootViewController
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
self.rv = [[RootView alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
}
return self;
}
- (void)viewDidLoad {
[super viewDidLoad];
self.rv.scrollV.contentSize = CGSizeMake([UIScreen mainScreen].bounds.size.width * 6, 200);
[self addImage];
//设置代理
self.rv.scrollV.delegate = self;
//开启定时器
[self addTimer];
}
-(void)loadView
{
self.view = self.rv;
}
//添加图片
-(void)addImage
{
for (int i = 0; i < 6; i++) {
UIImageView *imaV = [[UIImageView alloc] initWithImage:[UIImage imageNamed:[NSString stringWithFormat:@"%d.png",i]]];
imaV.frame = CGRectMake([UIScreen mainScreen].bounds.size.width * i, 0, [UIScreen mainScreen].bounds.size.width, 200);
imaV.userInteractionEnabled = YES;
[self.rv.scrollV addSubview:imaV];
}
}
//滚动结束的时候
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
// 计算页码
// 页码 = (contentoffset.x + scrollView一半宽度)/scrollView宽度
CGFloat scrollviewW = scrollView.frame.size.width;
CGFloat x = scrollView.contentOffset.x;
int page = (x + scrollviewW / 2) / scrollviewW;
self.rv.pageC.currentPage = page;
}
//自动轮播下一页 当到最后一页时 让page=0
-(void)nextImage
{
//获取当前的page数
int page = (int)self.rv.pageC.currentPage;
if (page == 5) {
page = 0;
}else{
page++;
}
//根据页数确定contentOffset
self.rv.scrollV.contentOffset = CGPointMake(page * [UIScreen mainScreen].bounds.size.width,0);
}
//将要拖拽时 停止定时器(即删除,因为定时器一旦暂停就不能重新开启)
-(void)scrollViewWillBeginDragging:(UIScrollView *)scrollView{
[self deleteTimer];
}
//拖拽结束 开启定时器
-(void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate{
[self addTimer];
}
/**
* 开启定时器
* NSTimer其实是将一个监听加入到系统的RunLoop中去,当系统runloop监听到timer的循环 时,会调用timer一次,当timer执行完,也就是回调函数执行之后,timer会再一次的将自己加入到runloop中去继续监听。
* 定时器NSTimer
* 定时器:适合用来隔一段时间做一些间隔比较长的操
* NSTimeInterval:隔多长时间操作一次
* target :操作的对象
* selector:调用的方法
* userInfo: 传递参数
* repeats: 是否重复
*/
-(void)addTimer
{
self.timer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(nextImage) userInfo:nil repeats:YES];
[[NSRunLoop currentRunLoop] addTimer:self.timer forMode:NSRunLoopCommonModes];
}
//删除定时器方法
-(void) deleteTimer{
[_timer invalidate];
_timer = nil;
}