瞭解iPhone應用中UIScrollView的使用方法

來源:互聯網
上載者:User

瞭解iPhone應用中UIScrollView的使用方法是本文要介紹的內容,主要是實現UIScrollView可以產生畫軸的效果,可以在手機屏中左右滑動。先來看內容。

初始化代碼為:

 
  1. mainView = [[UIScrollView alloc] initWithFrame:CGRectMake(0.0, 0.0, self.view.frame.size.width, 400)];  
  2. mainView.directionalLockEnabled = YES;  
  3. mainView.pagingEnabled = YES;  
  4. mainView.backgroundColor = [UIColor blueColor];  
  5. mainView.showsVerticalScrollIndicator = NO;  
  6. mainView.showsHorizontalScrollIndicator = NO;  
  7. mainView.delegate = self;  
  8. CGSize newSize = CGSizeMake(self.view.frame.size.width * 2,  self.view.frame.size.height);  
  9. [mainView setContentSize:newSize];  
  10. [self.view addSubview:mainView];  
  11. pageControl = [[UIPageControl alloc] initWithFrame:CGRectMake(0.0, 401, self.view.frame.size.width, 80)];  
  12. pageControl.hidesForSinglePage = YES;  
  13. pageControl.userInteractionEnabled = NO;  
  14. pageControl.backgroundColor = [UIColor redColor];  
  15. [self.view addSubview:pageControl];  
  16. UIView *view1=[[UIView alloc] initWithFrame:CGRectMake(0.0, 0.0, self.view.frame.size.width, self.view.frame.size.height)];  
  17. view1.backgroundColor=[UIColor redColor];  
  18. [mainView addSubview:view1];  
  19. UIView *view2=[[UIView alloc] initWithFrame:CGRectMake(self.view.frame.size.width, 0.0, self.view.frame.size.width, self.view.frame.size.height)];  
  20. view2.backgroundColor=[UIColor blueColor];  
  21. [mainView addSubview:view2];  

滑動的事件為:

 
  1. #pragma mark -  
  2. #pragma mark UIScrollView  
  3. - (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{  
  4.  if ([scrollView isKindOfClass:[UITableView class]]) {  
  5.   return;  
  6.  }  
  7.    
  8.  int index = fabs(scrollView.contentOffset.x) / scrollView.frame.size.width;  
  9.    
  10.  pageControl.currentPage = index;  
  11.  
  12.  //index為當前頁碼  
  13.  NSLog(@"%d",index);  

小結:瞭解iPhone應用中UIScrollView的使用方法的內容介紹完了,希望本文對你有所協助!

聯繫我們

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