UIScrollView UIPageControl 的使用

本文介绍如何使用 iOS 中的 UIScrollView 控件实现平滑的页面切换效果。通过设置 scrollView 的属性,添加子视图并利用 UIPageControl 进行页面指示,实现了流畅的水平滚动效果。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

//    

//            scrollView.contentSize = CGSizeMake(self.view.frame.size.width * 10, self.view.frame.size.height);

//        [self.view addSubview:scrollView];

//        scrollView.delegate = self;

//        scrollView.pagingEnabled = YES;

//        scrollView.showsHorizontalScrollIndicator = NO;

//        for (int i = 0; i < 10; i++) {

//               UIImage *image = [UIImage imageNamed:@"Default"];

//               UIImageView *imageView = [[UIImageView alloc] initWithImage:image];

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

//                [scrollView addSubview:imageView];

//

    

   mainScrollView.contentSize=CGSizeMake(self.view.frame.size.width * 2self.view.frame.size.height);

   //[self.view addSubview:mainScrollView];

    [mainScrollViewsetDelegate:self];

   mainScrollView.pagingEnabled =YES;

   mainScrollView.showsHorizontalScrollIndicator =NO;

    

   viewOne.frame =CGRectMake(self.view.frame.size.width * 00self.view.frame.size.width,self.view.frame.size.height);

    [mainScrollViewaddSubview:viewOne];

    

   viewTwo.frame =CGRectMake(self.view.frame.size.width * 10self.view.frame.size.width,self.view.frame.size.height);

    [mainScrollViewaddSubview:viewTwo];



  

iPhone 使用UIScrollView实现滚屏效果



程序要像Spring一样在一屏中手指滑动实现多个页面的滑动切换可使用UIScrollView来实现

 

·········10········20········30········40········50········60········70········80········90········100·······110·······120·······130·······140·······150
  1. - (void)viewDidLoad  
  2. {  
  3.     [super viewDidLoad];  
  4.       
  5.     _scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0.0, 0.0, self.view.frame.size.width, self.view.frame.size.height)];  
  6.     _scrollView.userInteractionEnabled = YES;  
  7.     _scrollView.directionalLockEnabled = YES;  
  8.     _scrollView.pagingEnabled = YES;  
  9.     _scrollView.showsVerticalScrollIndicator = NO;  
  10.     _scrollView.showsHorizontalScrollIndicator = NO;  
  11.     _scrollView.delegate = self;  
  12.       
  13.     CGRect frame = self.view.frame;  
  14.     frame.origin.y = 0.0f;  
  15.     UIView* viewFirst = [[[UIView alloc] initWithFrame:frame] autorelease];  
  16.     viewFirst.backgroundColor = [UIColor greenColor];  
  17.       
  18.     frame.origin.x += self.view.frame.size.width;  
  19.     UIView* viewSecond = [[[UIView alloc] initWithFrame:frame] autorelease];  
  20.     viewSecond.backgroundColor = [UIColor blueColor];  
  21.       
  22.     frame.origin.x += self.view.frame.size.width;  
  23.     UIView* viewThird = [[[UIView alloc] initWithFrame:frame] autorelease];  
  24.     viewThird.backgroundColor = [UIColor redColor];  
  25.     _scrollView.contentSize = CGSizeMake(self.view.frame.size.width*3, self.view.frame.size.height);  
  26.     [_scrollView addSubview:viewFirst];  
  27.     [_scrollView addSubview:viewSecond];  
  28.     [_scrollView addSubview:viewThird];  
  29.       
  30.     CGSize sizePageControl = CGSizeMake(120, 40);  
  31.     CGRect framePageControl = CGRectMake((self.view.frame.size.width-sizePageControl.width)/2, (self.view.frame.size.height-sizePageControl.height-40), sizePageControl.width, sizePageControl.height);  
  32.     _pageControl = [[UIPageControl alloc] initWithFrame:framePageControl];  
  33.     _pageControl.hidesForSinglePage = YES;  
  34.     _pageControl.userInteractionEnabled = NO;  
  35.     _pageControl.backgroundColor = [UIColor clearColor];  
  36.     _pageControl.numberOfPages = 3;  
  37.       
  38.     [self.view addSubview:_scrollView];  
  39.     [self.view addSubview:_pageControl];  
  40.       
  41. }  

同时你的ViewController 要实现UIScrollViewDelegate 协议的如下方法, 根据scroll 更新UIPageControl(当然如过你用到UIPageControl的话就无所谓了...)

 

·········10········20········30········40········50········60········70········80········90········100·······110·······120·······130·······140·······150
  1. - (void) scrollViewDidEndDecelerating:(UIScrollView *)scrollView  
  2. {  
  3.     int index = fabs(_scrollView.contentOffset.x)/self.view.frame.size.width;  
  4.       
  5.     _pageControl.currentPage = index;  
  6. }  

 

下面是效果图:

 

 

部分 转至http://blog.youkuaiyun.com/z888c/article/details/6563662

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值