ios之UIPageController和UIScrollView配合使用

來源:互聯網
上載者:User

標籤:

UIPageController

  UIPageController控制項主要用來實現視圖分頁,通常會和UIScrollView配合使用

 建立

      UIPageControl *page=[[UIPageControl alloc] init];

 設定frame

  pagel.frame = CGRectMake(0,self.view.frame.size.height -40,self.view.frame.size.width,20);

常用屬性 

分頁面的數量

  page.numberOfPages = self.imageNames.count;

設定翻頁標渲染顏色

  page.pageIndicatorTintColor = [UIColor  blackColor];

設定當前頁的渲染顏色

  pagel.currentPageIndicatorTintColor =[UIColor  redColor];

關閉使用者互動

  page.userInteractionEnabled = NO;

 添加到父視圖

  [self.view  addSubview:page];

 

UIPageController和UIScrollView配合使用

   首先需要遵守<UIScrollViewDelegate>協議,然後在viewDidLoad:方法裡面給delegate賦值,

  最後實現協議中的scrollViewDidScroll:方法,該方法在每次完成滾動的時候都會被調用,即在該方法裡面計算出當前顯示第幾頁並賦值給self.pageControl.currentPage

 

- (void)viewDidLoad

{

      [super viewDidLoad];

      UIScrollView *scrollView=[[UIScrollView alloc] init];

      CGRect frame=self.view.frame;

      scrollView.frame=frame;

      UIImage *image1=[UIImage imageNamed:@"1.jpg"];

      UIImage *image2=[UIImage imageNamed:@"2.jpg"];

      UIImage *image3=[UIImage imageNamed:@"3.jpg"];

      [email protected][image1,image2,image3];

     CGSize   size=CGSizeMake(self.array.count*self.view.frame.size.width, self.view.frame.size.height);

     //顯示內容大小

     scrollView.contentSize=size;

     for(int i=0;i<self.array.count;i++)

     {

           UIImageView *imageView=[[UIImageView alloc] initWithImage:self.array[i]];

    //圖片顯示形式

          imageView.contentMode=UIViewContentModeScaleAspectFit;

     //圖片顯示範圍

          imageView.frame=CGRectMake(i*self.view.frame.size.width, 0, self.view.frame.size.width, self.view.frame.size.height);

          //加到scrollView中

          [scrollView addSubview:imageView];

}

    //整頁翻動

      scrollView.pagingEnabled=YES;

    UIPageControl *page=[[UIPageControl alloc] init];

    self.page=page;

   //位置

      page.frame = CGRectMake(0,self.view.frame.size.height - 40, self.view.frame.size.width,20);

      //頁面個數

    page.numberOfPages=self.array.count;

       //設定顏色

   page.currentPageIndicatorTintColor=[UIColor redColor];

   page.pageIndicatorTintColor=[UIColor blackColor];

  //將自己設定成.delegate

    scrollView.delegate=self;

  //加到view裡

     [self.view addSubview:scrollView];

       [self.view addSubview:page];

}

 

   //實現方法

    //最後實現page的指示標配合scrollView的滾動顯示功能,此功能需要通過scrollView的delegate來實現。

-(void)scrollViewDidScroll:(UIScrollView *)scrollView

{

   //得到圖片移動相對原點的座標

  CGPoint point=scrollView.contentOffset

  //移動不能超過左邊;

    if(point.x<0)

    {

        point.x=0;

        scrollView.contentOffset=point;

}

  //移動不能超過右邊

      if(point.x>(self.array.count-1)*self.view.frame.size.width)

    {

        point.x=self.view.frame.size.width*(self.array.count-1);

        scrollView.contentOffset=point;

}

  //根據圖片座標判斷頁數

      NSInteger index=round(point.x/self.view.frame.size.width);

      self.page.currentPage=index;

}

 

 

 

聲明:參考資料達內筆記

ios之UIPageController和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.