ios輪播
阿新 • • 發佈:2018-02-28
ring nta div setup selector ner copyright float horizon
// // 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
ios輪播