iPhone 使用UIScrollView實現Spring滾屏效果

來源:互聯網
上載者:User

程式要像Spring一樣在一屏中手指滑動實現多個頁面的滑動切換可使用UIScrollView來實現

 

- (void)viewDidLoad<br />{<br /> [super viewDidLoad];</p><p> _scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0.0, 0.0, self.view.frame.size.width, self.view.frame.size.height)];<br /> _scrollView.userInteractionEnabled = YES;<br /> _scrollView.directionalLockEnabled = YES;<br /> _scrollView.pagingEnabled = YES;<br /> _scrollView.showsVerticalScrollIndicator = NO;<br /> _scrollView.showsHorizontalScrollIndicator = NO;<br /> _scrollView.delegate = self;</p><p> CGRect frame = self.view.frame;<br /> frame.origin.y = 0.0f;<br /> UIView* viewFirst = [[[UIView alloc] initWithFrame:frame] autorelease];<br /> viewFirst.backgroundColor = [UIColor greenColor];</p><p> frame.origin.x += self.view.frame.size.width;<br /> UIView* viewSecond = [[[UIView alloc] initWithFrame:frame] autorelease];<br /> viewSecond.backgroundColor = [UIColor blueColor];</p><p> frame.origin.x += self.view.frame.size.width;<br /> UIView* viewThird = [[[UIView alloc] initWithFrame:frame] autorelease];<br /> viewThird.backgroundColor = [UIColor redColor];<br /> _scrollView.contentSize = CGSizeMake(self.view.frame.size.width*3, self.view.frame.size.height);<br /> [_scrollView addSubview:viewFirst];<br /> [_scrollView addSubview:viewSecond];<br /> [_scrollView addSubview:viewThird];</p><p> CGSize sizePageControl = CGSizeMake(120, 40);<br /> CGRect framePageControl = CGRectMake((self.view.frame.size.width-sizePageControl.width)/2, (self.view.frame.size.height-sizePageControl.height-40), sizePageControl.width, sizePageControl.height);<br /> _pageControl = [[UIPageControl alloc] initWithFrame:framePageControl];<br /> _pageControl.hidesForSinglePage = YES;<br /> _pageControl.userInteractionEnabled = NO;<br /> _pageControl.backgroundColor = [UIColor clearColor];<br /> _pageControl.numberOfPages = 3;</p><p> [self.view addSubview:_scrollView];<br /> [self.view addSubview:_pageControl];</p><p>}<br />

同時你的ViewController 要實現UIScrollViewDelegate 協議的如下方法, 根據scroll 更新UIPageControl(當然如過你用到UIPageControl的話就無所謂了...)

 

- (void) scrollViewDidEndDecelerating:(UIScrollView *)scrollView<br />{<br /> int index = fabs(_scrollView.contentOffset.x)/self.view.frame.size.width;</p><p> _pageControl.currentPage = index;<br />}<br />

 

下面是:

 

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.