温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

怎么在iOS中使用UIScrollView实现无限循环轮播图

发布时间:2021-05-31 17:47:41 来源:亿速云 阅读:149 作者:Leah 栏目:移动开发

怎么在iOS中使用UIScrollView实现无限循环轮播图?针对这个问题,这篇文章详细介绍了相对应的分析和解答,希望可以帮助更多想解决这个问题的小伙伴找到更简单易行的方法。

代码:

 
#import "ViewController.h"
 
@interface ViewController ()<UIScrollViewDelegate>
/* 定时器 */
@property(nonatomic,strong)NSTimer *rotateTimer;
/* */
@property(nonatomic,strong)UIPageControl *myPageControl;
@end
 
@implementation ViewController
 
- (void)viewDidLoad {
 [super viewDidLoad];
 //初始化scroolview大小为屏幕大小
 UIScrollView *rotateScrollView = [[UIScrollView alloc]initWithFrame:self.view.frame];
 //设置滚动范围
 rotateScrollView.contentSize = CGSizeMake(CGRectGetWidth(self.view.frame)*3, CGRectGetHeight(self.view.frame));
 //设置分页效果
 rotateScrollView.pagingEnabled = YES;
 //水平滚动条隐藏
 rotateScrollView.showsHorizontalScrollIndicator = NO;
 //添加三个子视图,uilabel类型
 for (int i=0; i<3; i++) {
 UILabel *subLabel = [[UILabel alloc] initWithFrame:CGRectMake(CGRectGetWidth(self.view.frame)*i, 0, CGRectGetWidth(self.view.frame), CGRectGetHeight(self.view.frame))];
 subLabel.tag = 1000+i;
 subLabel.text = [NSString stringWithFormat:@"我是第%d个视图",i];
 [subLabel setFont:[UIFont systemFontOfSize:80]];
 subLabel.adjustsFontSizeToFitWidth = YES;
 [subLabel setBackgroundColor:[UIColor colorWithRed:arc4random()%256/255.0 green:arc4random()%256/255.0 blue:arc4random()%256/255.0 alpha:1.0]];
 [rotateScrollView addSubview:subLabel];
 
 }
 UILabel *tempLabel = [rotateScrollView viewWithTag:1000];
 //为滚动视图的右边添加一个视图,使得它和第一个视图一模一样。
 UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(CGRectGetWidth(self.view.frame)*3, 0, CGRectGetWidth(self.view.frame), CGRectGetHeight(self.view.frame))];
 label.backgroundColor = tempLabel.backgroundColor;
 label.text = tempLabel.text;
 label.font = tempLabel.font;
 label.adjustsFontSizeToFitWidth = YES;
 [rotateScrollView addSubview:label];
 [self.view addSubview:rotateScrollView];
 rotateScrollView.tag = 1000;
 self.myPageControl = [[UIPageControl alloc]initWithFrame:CGRectMake(0, CGRectGetHeight(self.view.frame)-50, CGRectGetWidth(self.view.frame), 50)];
 self.myPageControl.numberOfPages = 3;
 self.myPageControl.currentPage = 0;
 [self.view addSubview:self.myPageControl];
 
 //启动定时器
 self.rotateTimer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(changeView) userInfo:nil repeats:YES];
 //为滚动视图指定代理
 rotateScrollView.delegate = self;
}
 
#pragma mark -- 滚动视图的代理方法
//开始拖拽的代理方法,在此方法中暂停定时器。
-(void)scrollViewWillBeginDragging:(UIScrollView *)scrollView{
 NSLog(@"正在拖拽视图,所以需要将自动播放暂停掉");
 //setFireDate:设置定时器在什么时间启动
 //[NSDate distantFuture]:将来的某一时刻
 [self.rotateTimer setFireDate:[NSDate distantFuture]];
}
 
//视图静止时(没有人在拖拽),开启定时器,让自动轮播
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
 //视图静止之后,过1.5秒在开启定时器
 //[NSDate dateWithTimeInterval:1.5 sinceDate:[NSDate date]] 返回值为从现在时刻开始 再过1.5秒的时刻。
 NSLog(@"开启定时器");
 [self.rotateTimer setFireDate:[NSDate dateWithTimeInterval:1.5 sinceDate:[NSDate date]]];
}
 
 
//定时器的回调方法 切换界面
- (void)changeView{
 //得到scrollView
 UIScrollView *scrollView = [self.view viewWithTag:1000];
 //通过改变contentOffset来切换滚动视图的子界面
 float offset_X = scrollView.contentOffset.x;
 //每次切换一个屏幕
 offset_X += CGRectGetWidth(self.view.frame);
 
 //说明要从最右边的多余视图开始滚动了,最右边的多余视图实际上就是第一个视图。所以偏移量需要更改为第一个视图的偏移量。
 if (offset_X > CGRectGetWidth(self.view.frame)*3) {
 scrollView.contentOffset = CGPointMake(0, 0);
 
 }
 //说明正在显示的就是最右边的多余视图,最右边的多余视图实际上就是第一个视图。所以pageControl的小白点需要在第一个视图的位置。
 if (offset_X == CGRectGetWidth(self.view.frame)*3) {
 self.myPageControl.currentPage = 0;
 }else{
 self.myPageControl.currentPage = offset_X/CGRectGetWidth(self.view.frame);
 }
 
 //得到最终的偏移量
 CGPoint resultPoint = CGPointMake(offset_X, 0);
 //切换视图时带动画效果
 //最右边的多余视图实际上就是第一个视图,现在是要从第一个视图向第二个视图偏移,所以偏移量为一个屏幕宽度
 if (offset_X >CGRectGetWidth(self.view.frame)*3) {
 self.myPageControl.currentPage = 1;
 [scrollView setContentOffset:CGPointMake(CGRectGetWidth(self.view.frame), 0) animated:YES];
 }else{
 [scrollView setContentOffset:resultPoint animated:YES];
 }
 
}
 
- (void)didReceiveMemoryWarning {
 [super didReceiveMemoryWarning];
 // Dispose of any resources that can be recreated.
}
 
 
@end

关于怎么在iOS中使用UIScrollView实现无限循环轮播图问题的解答就分享到这里了,希望以上内容可以对大家有一定的帮助,如果你还有很多疑惑没有解开,可以关注亿速云行业资讯频道了解更多相关知识。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI