国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發(fā)設計 > 正文

ios之UIPageController和UIScrollView配合使用

2019-11-14 18:21:30
字體:
來源:轉載
供稿:網(wǎng)友

UipageController

  UIPageController控件主要用來實現(xiàn)視圖分頁,通常會和UIScrollView配合使用

 創(chuàng)建

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

 設置frame

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

常用屬性 

分頁面的數(shù)量

  page.numberOfPages = self.imageNames.count;

設置翻頁標渲染顏色

  page.pageIndicatorTintColor = [UIColor  blackColor];

設置當前頁的渲染顏色

  pagel.currentPageIndicatorTintColor =[UIColor  redColor];

關閉用戶交互

  page.userInteractionEnabled = NO;

 添加到父視圖

  [self.view  addSubview:page];

 

UIPageController和UIScrollView配合使用

   首先需要遵守<UIScrollViewDelegate>協(xié)議,然后在viewDidLoad:方法里面給delegate賦值,

  最后實現(xiàn)協(xié)議中的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"];

      self.array=@[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);

      //頁面?zhèn)€數(shù)

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

}

 

   //實現(xiàn)方法

    //最后實現(xiàn)page的指示標配合scrollView的滾動顯示功能,此功能需要通過scrollView的delegate來實現(xiàn)。

-(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;

}

  //根據(jù)圖片坐標判斷頁數(shù)

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

      self.page.currentPage=index;

}

 

 

 

聲明:參考資料達內筆記


發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 靖远县| 神木县| 马关县| 岱山县| 嘉善县| 衡阳县| 鹤庆县| 瓦房店市| 礼泉县| 克山县| 沭阳县| 星座| 贵德县| 镇康县| 牡丹江市| 绍兴市| 两当县| 延津县| 阿拉尔市| 岳阳县| 札达县| 宿松县| 敖汉旗| 梨树县| 宜昌市| 涞源县| 连江县| 乐安县| 丽水市| 武夷山市| 张家口市| 探索| 临沧市| 通山县| 梅河口市| 聂拉木县| 连山| 抚松县| 化州市| 江油市| 余庆县|