ios开发之 -- 自动轮播图创建
生活随笔
收集整理的這篇文章主要介紹了
ios开发之 -- 自动轮播图创建
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
這里是oc版本的,簡單記錄下:
具體代碼如下:
1,準備
#define FRAME [[UIScreen mainScreen] bounds] #define WIDTH FRAME.size.width #define HEIGHT FRAME.size.height2,具體實現
//scrollview的添加_bigScrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 20, WIDTH, HEIGHT)];_bigScrollView.showsHorizontalScrollIndicator = NO;_bigScrollView.contentSize = CGSizeMake(WIDTH *6, 0);_bigScrollView.pagingEnabled = YES;_bigScrollView.bounces = NO;_bigScrollView.delegate = self;[self.view addSubview:_bigScrollView];//圖片內容的添加for ( int i = 0; i<6; i++) {UIImageView *imageView = [[UIImageView alloc]initWithFrame:CGRectMake(i *WIDTH, 0, WIDTH, HEIGHT)];imageView.image = [UIImage imageNamed:[NSString stringWithFormat:@"%d.jpg",i]];[_bigScrollView addSubview:imageView];}//pagecontrol的創建_pageControl = [[UIPageControl alloc] initWithFrame:CGRectMake(0, 0, WIDTH, 40)] ;_pageControl.center = CGPointMake(_bigScrollView.center.x, _bigScrollView.center.y);_pageControl.numberOfPages = 6;_pageControl.currentPage = 0;_pageControl.pageIndicatorTintColor = [UIColor blueColor];_pageControl.currentPageIndicatorTintColor =[UIColor redColor];[self.view addSubview:_pageControl];[_pageControl addTarget:self action:@selector(pageControllerClick) forControlEvents:UIControlEventValueChanged];//定時器的創建_timer = [NSTimer scheduledTimerWithTimeInterval:2 target:self selector:@selector(onTimer) userInfo:nil repeats:YES];3,響應方法的實現
-(void)pageControllerClick {[_bigScrollView setContentOffset:CGPointMake(_pageControl.currentPage*320, 0) animated:YES]; }//在scrollview開始手動滑動的時候 - (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView {[_timer setFireDate:[NSDate distantFuture]]; }//在scrollview添加一個延遲方法 - (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {_pageControl.currentPage = _bigScrollView.contentOffset.x/WIDTH;[self performSelector:@selector(disPast) withObject:nil afterDelay:2];//延遲方法 }//啟動定時器 -(void)disPast {[_timer setFireDate:[NSDate distantPast]]; }static int count=1;//定時器的具體響應方法 -(void)onTimer {_pageControl.currentPage+=count;if (_pageControl.currentPage==_pageControl.numberOfPages-1||_pageControl.currentPage==0){count=-count;};[_bigScrollView setContentOffset:CGPointMake(_pageControl.currentPage*WIDTH, 0) animated:YES];}效果圖就不加了,這里僅做記錄用!
轉載于:https://www.cnblogs.com/hero11223/p/7467255.html
總結
以上是生活随笔為你收集整理的ios开发之 -- 自动轮播图创建的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: POJ-3624 Charm Brace
- 下一篇: 定编定岗定员方案_定岗、定编、定员实施方