iOS轮播图的实现
//使用轮播图展示一组图片信息
//定时器 使循环
[NSTimer scheduledTimerWithTimeInterval:3 target:self selector:@selector(runTimePage) userInfo:nil repeats:YES];
//初始化scrollview
self.scrollview = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 64, 320, 200)];
self.scrollview.bounces = YES;
self.scrollview.pagingEnabled = YES;
self.scrollview.delegate = self;
self.scrollview.userInteractionEnabled = YES;
self.scrollview.showsHorizontalScrollIndicator = NO;
self.scrollview.showsVerticalScrollIndicator = NO;
[self.homeView addSubview:self.scrollview];
//初始化存放循环图片的数组
self.slideImages = [NSMutableArray array];
[self.slideImages addObject:@"1.jpg"];
[self.slideImages addObject:@"2.jpg"];
[self.slideImages addObject:@"3.jpg"];
[self.slideImages addObject:@"4.jpg"];
//初始化pageControl
self.pageControl = [[UIPageControl alloc]initWithFrame:CGRectMake(110,240.0,100.0,18.0)]; // 初始化mypagecontrol
[self.pageControl setCurrentPageIndicatorTintColor:[UIColor redColor]];
[self.pageControl setPageIndicatorTintColor:[UIColor blackColor]];
self.pageControl.numberOfPages = [self.slideImages count];
self.pageControl.currentPage = 0;
[self.pageControl addTarget:self action:@selector(turnPage) forControlEvents:UIControlEventValueChanged]; // 触摸mypagecontrol触发change这个方法事件
[self.homeView addSubview:self.pageControl];
// 创建四个图片 imageviewm,
for (int i = 0;i<[self.slideImages count];i++)
{
UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:[self.slideImages objectAtIndex:i]]];
imageView.frame = CGRectMake((320 * i) + 320, 0, 320, 190.0);
UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(didClickTapGesture:)];
imageView.tag = i + 100;
imageView.userInteractionEnabled = YES;
[imageView addGestureRecognizer:tapGesture];
[self.scrollview addSubview:imageView]; // 首页是第0页,默认从第1页开始的。所以+320。。。
}
// 取数组最后一张图片 放在第0页
UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:[self.slideImages objectAtIndex:([self.slideImages count]-1)]]];
imageView.frame = CGRectMake(0, 0, 320, 190.0); // 添加最后1页在首页 循环
[self.scrollview addSubview:imageView];
// 取数组第一张图片 放在最后1页
imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:[self.slideImages objectAtIndex:0]]];
imageView.frame = CGRectMake((320 * ([self.slideImages count] + 1)) , 0, 320, 190.0); // 添加第1页在最后 循环
[self.scrollview addSubview:imageView];
[self.scrollview setContentSize:CGSizeMake(320 * ([self.slideImages count] + 2), 190.0
)]; // +上第1页和第4页 原理:4-[1-2-3-4]-1
[self.scrollview setContentOffset:CGPointMake(0, 0)];
[self.scrollview scrollRectToVisible:CGRectMake(320,64,320,190.0) animated:NO]; // 默认从序号1位置放第1页 ,序号0位置位置放第4页
#pragma -------------------------------轮播图-----------------------------------------
// scrollview 委托函数
- (void)scrollViewDidScroll:(UIScrollView *)sender
{
CGFloat pagewidth = self.scrollview.frame.size.width;
int page = floor((self.scrollview.contentOffset.x - pagewidth/([self.slideImages count]+2))/pagewidth)+1;
page --; // 默认从第二页开始
self.pageControl.currentPage = page;
}
// scrollview 委托函数
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
CGFloat pagewidth = self.scrollview.frame.size.width;
int currentPage = floor((self.scrollview.contentOffset.x - pagewidth/ ([self.slideImages count]+2)) / pagewidth) + 1;
// int currentPage_ = (int)self.scrollView.contentOffset.x/320; // 和上面两行效果一样
// NSLog(@"currentPage_==%d",currentPage_);
if (currentPage==0)
{
[self.scrollview scrollRectToVisible:CGRectMake(320 * [self.slideImages count],64,320,190.0) animated:NO]; // 序号0 最后1页
}
else if (currentPage==([self.slideImages count]+1))
{
[self.scrollview scrollRectToVisible:CGRectMake(320,0,320,190.0) animated:NO]; // 最后+1,循环第1页
}
}
// pagecontrol 选择器的方法
- (void)turnPage
{
int page = self.pageControl.currentPage; // 获取当前的page
[self.scrollview scrollRectToVisible:CGRectMake(320*(page+1),0,320,190.0) animated:NO]; // 触摸pagecontroller那个点点 往后翻一页 +1
}
// 定时器 绑定的方法
- (void)runTimePage
{
int page = self.pageControl.currentPage; // 获取当前的page
page++;
page = page > 3 ? 0 : page ;
self.pageControl.currentPage = page;
[self turnPage];
}
//图片手势
- (void)didClickTapGesture:(UITapGestureRecognizer *)gesture
{
UIImageView * imageView = nil;
switch (gesture.view.tag) {
case 101:
imageView = (UIImageView *)[self.scrollview viewWithTag:101];
break;
case 102:
imageView = (UIImageView *)[self.scrollview viewWithTag:102];
break;
case 103:
imageView = (UIImageView *)[self.scrollview viewWithTag:103];
break;
default:
break;
}
}
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。