很多App里面都有轮播图,所以自己也试着写了一个,如果有什么不完善的地方,欢迎指点。
1.创建文件,RootView,RootViewController;
2.在RootView.h申明属性,.m里面设置:代码如下
#import "RootView.h"
#define Swidth CGRectGetWidth([UIScreen mainScreen].bounds)
#define sHeight CGRectGetHeight([UIScreen mainScreen].bounds)
@implementation RootView
- (instancetype)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
[self setupView];
}
return self;
}
- (void)setupView{
self.backgroundColor = [UIColor whiteColor];
self.scrollView = [[UIScrollView alloc] initWithFrame:[UIScreen mainScreen].bounds];
// 设置包含空间的大小
self.scrollView.contentSize = CGSizeMake(Swidth * 10, sHeight);
self.scrollView.pagingEnabled = YES;
self.scrollView.showsHorizontalScrollIndicator = NO;
self.scrollView.contentOffset = CGPointMake(Swidth, 0);
[self addSubview:self.scrollView];
//添加图片
[self addImage:self.scrollView];
// pageControl
self.page = [[UIPageControl alloc] initWithFrame:CGRectMake(0, sHeight - 50, Swidth, 50)];
self.page.numberOfPages = 8;
self.page.backgroundColor = [UIColor colorWithRed:0.000 green:0.502 blue:0.502 alpha:0.5];
[self addSubview:self.page];
}
- (void)addImage:(UIView *)view{
// UIImageView *imageView0 = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"7.jpg"]];
// imageView0.frame = CGRectMake(0, 0, Swidth, sHeight);
// [view addSubview:imageView0];
for (int i = 0; i < 10; i++) {
UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:[NSString stringWithFormat:@"%d.jpg", i]]];
imageView.frame = CGRectMake(Swidth * i, 0, Swidth, sHeight);
[view addSubview:imageView];
}
// UIImageView *imageView1 = [[UIImageView alloc] initWithFrame:CGRectMake(Swidth * 9, 0, Swidth, sHeight)];
// imageView1.image = [UIImage imageNamed:@"0.jpg"];
// [view addSubview:imageView1];
}
3.在主函数里面实现
#import "RootViewController.h"
#define Swidth CGRectGetWidth([UIScreen mainScreen].bounds)
#define sHeight CGRectGetHeight([UIScreen mainScreen].bounds)
@interface RootViewController ()<UIScrollViewDelegate>
@property (nonatomic, strong)NSTimer *timer;
@end
@implementation RootViewController
- (void)loadView{
[super loadView];
self.rv = [[RootView alloc] initWithFrame:[UIScreen mainScreen].bounds];
self.view = self.rv;
}
- (void)viewDidLoad {
[super viewDidLoad];
self.rv.scrollView.delegate = self;
[self.rv.page addTarget:self action:@selector(pageChange:) forControlEvents:UIControlEventValueChanged];
self.timer = [NSTimer timerWithTimeInterval:4.0 target:self selector:@selector(nextImage) userInfo:nil repeats:YES];
NSRunLoop *runLoop = [NSRunLoop currentRunLoop];
[runLoop addTimer:self.timer forMode:NSRunLoopCommonModes];
// Do any additional setup after loading the view.
}
- (void)pageChange:(UIPageControl *)page{
self.rv.scrollView.contentOffset = CGPointMake(Swidth * (page.currentPage + 1), 0);
[self.timer invalidate];
}
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
int page = scrollView.contentOffset.x / Swidth;
// NSLog(@"第%d张偏移", page);
self.rv.page.currentPage = page - 1;
if (page == 0) {
self.rv.scrollView.contentOffset = CGPointMake(8 * Swidth, 0);
self.rv.page.currentPage = 7;
return;
}
if (page == 9) {
self.rv.scrollView.contentOffset = CGPointMake(Swidth, 0);
self.rv.page.currentPage = 0;
return;
}
}
- (void)nextImage
{
NSInteger page = self.rv.page.currentPage + 1;
if (page == self.rv.page.numberOfPages ) {
page = 1;
}else{
page++;
}
[UIView animateWithDuration:1.0 animations:^{
self.rv.scrollView.contentOffset = CGPointMake(page * Swidth, 0);
}];
}
- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView{
[self.timer invalidate];
}
- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate{
self.timer = [NSTimer scheduledTimerWithTimeInterval:2.0 target:self selector:@selector(nextImage) userInfo:nil repeats:YES];
}
- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
int page = scrollView.contentOffset.x / Swidth;
self.rv.page.currentPage = page - 1;
if (page == 0) {
self.rv.scrollView.contentOffset = CGPointMake(8 * Swidth, 0);
self.rv.page.currentPage = 7;
}
if (page == 9) {
self.rv.scrollView.contentOffset = CGPointMake(Swidth, 0);
self.rv.page.currentPage = 0;
}
}