ios開發之 -- 自動輪播圖建立

來源:互聯網
上載者:User

標籤:ini   簡單   ble   ios開發   開始      方法   val   color   

這裡是oc版本的,簡單記錄下:

具體代碼如下:

1,準備

#define FRAME [[UIScreen mainScreen] bounds]#define WIDTH  FRAME.size.width#define HEIGHT FRAME.size.height

2,具體實現

 //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];    }

就不加了,這裡僅做記錄用!

ios開發之 -- 自動輪播圖建立

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.