// // ViewController.m // Ocproject // // Created by wenzhe yi on 2018/2/28. // Copyright ? 2018年 wenzhe yi. All rights reserved. // #import "Shuanghui.h" #import "ViewController.h" #define imageCount 5 #define kscrollviewsize (_scrollView.frame.size) @interface ViewController()<UIScrollViewDelegate> @property (weak, nonatomic) IBOutlet UIScrollView *scrollView; @property (weak, nonatomic) IBOutlet UIPageControl *pageController; @property(strong,nonatomic) NSTimer *timer; @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view, typically from a nib. [self initUi]; [self initPageController]; [self initTimer]; } -(void)initTimer{ _timer = [NSTimer scheduledTimerWithTimeInterval:3 target:self selector:@selector(autoBanner) userInfo:nil repeats:YES]; } -(void)initUi{ [self initScrollView]; } //开始拖拽的时候计时器停止 -(void)scrollViewWillBeginDragging:(UIScrollView *)scrollView{ [_timer invalidate]; } //拖拽结束的时候在开启定时器 -(void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate{ [self initTimer]; } -(void)initPageController{ _pageController.numberOfPages=imageCount; //非选中颜色 _pageController.pageIndicatorTintColor=[UIColor grayColor]; //选中的颜色 _pageController.currentPageIndicatorTintColor=[UIColor redColor]; //当前页 _pageController.currentPage=0; } -(void)initScrollView{ // UIImageView *iv = [[UIImageView alloc]initWithFrame:_scrollView.bounds]; // // //设图片 // iv.image=[UIImage imageNamed:@"img_01"]; // // //添加 // [_scrollView addSubview:iv]; // CGSize scrollViewSize=_scrollView.frame.size; for (int i=0; i<imageCount; i++) { CGFloat imageX=i*kscrollviewsize.width; UIImageView *iv =[[UIImageView alloc] initWithFrame:CGRectMake(imageX, 0, kscrollviewsize.width, kscrollviewsize.height)]; //imagepath string NSString *path=[NSString stringWithFormat:@"img_%02d",i+1]; //设置pic iv.image=[UIImage imageNamed:path]; //add [_scrollView addSubview:iv]; } _scrollView.contentSize=CGSizeMake(5*kscrollviewsize.width, 0); //隐藏滚动条 _scrollView.showsHorizontalScrollIndicator=NO; //分页效果 _scrollView.pagingEnabled=YES; //代理 _scrollView.delegate=self; } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } -(void)needMoreMeet{ NSLog(@"I LIST"); } -(void)scrollViewDidScroll:(UIScrollView *)scrollView{ } -(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{ _pageController.currentPage=scrollView.contentOffset.x/kscrollviewsize.width; } //无线循环轮播点击事件 - (IBAction)move:(id)sender { [self autoBanner]; } -(void)autoBanner{ CGPoint offset=_scrollView.contentOffset; NSInteger currentPage = _pageController.currentPage; if(currentPage==imageCount-1){ currentPage=0; offset=CGPointZero; }else{ currentPage+=1; offset.x+=kscrollviewsize.width; } _pageController.currentPage=currentPage; [_scrollView setContentOffset:offset animated:YES]; } @end