首页 > 代码库 > 可以循环滚动的展示图
可以循环滚动的展示图
ViewController.h
@interface ViewController : UIViewController<UIScrollViewDelegate> { NSInteger _index; UIPageControl *_pageCtrl; UIScrollView *_scrollerView; }
ViewController.m
#import "ViewController.h" @interface ViewController () @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; _scrollerView= [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, 320, 480)]; _scrollerView.delegate = self; _scrollerView.backgroundColor = [UIColor redColor]; _scrollerView.pagingEnabled = YES; _scrollerView.showsHorizontalScrollIndicator = NO; _scrollerView.contentSize = CGSizeMake(320*6, 480); [self.view addSubview:_scrollerView]; for (int i=0; i<6; i++) { NSString *name = [NSString stringWithFormat:@"%d",i]; UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:name]]; imageView.frame = CGRectMake(320*i, 0, 320, 480); [_scrollerView addSubview:imageView]; } _pageCtrl = [[UIPageControl alloc] initWithFrame:CGRectMake(20, 400, 280, 30)]; _pageCtrl.backgroundColor = [UIColor grayColor]; _pageCtrl.numberOfPages = 5; [self.view addSubview:_pageCtrl]; //自动滑动 [NSTimer scheduledTimerWithTimeInterval:2 target:self selector:@selector(timeAction:) userInfo:nil repeats:YES]; } - (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView { int count = scrollView.contentOffset.x/320; //实现循环滑动 if (count == 5) { //重点是这句话 scrollView.contentOffset = CGPointMake(0, 0); _pageCtrl.currentPage = 0; }else { _pageCtrl.currentPage = count; } } - (void)timeAction:(NSTimer *)time { _index ++; if (_index == 5) { _index = 0; } [UIView beginAnimations:nil context:nil]; [UIView setAnimationDuration:.5]; _scrollerView.contentOffset = CGPointMake(_index*320, 0); [UIView commitAnimations]; _pageCtrl.currentPage = _scrollerView.contentOffset.x/320; } @end
可以循环滚动的展示图
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。