UIPageControl + UIScrollView 创建一个页面指示器控件

    UIPageControl 类提供了一行点来指示当前显示的是多页面视图的哪一页。点击当前颜色鲜艳的页面指示器的左边或右边,会触发 UIControlEventValueChanged 事件,

并启动设置为控件动作的任何方法。你可以通过调用currentPage 查询控件的新值,并通过调整 numberOfPage 属性设置可用的页面数。


代码 :使用UIPageControl 指示器

#define BARBUTTON(TITLE, SELECTOR)     [[[UIBarButtonItem alloc] initWithTitle:TITLE style:UIBarButtonItemStylePlain target:self action:SELECTOR] autorelease]
#define RSTRING(X) NSStringFromCGRect(X)

#define BASEHEIGHT    284.0f
#define NPAGES        3

- (void) pageTurn: (UIPageControl *) aPageControl
{
    int whichPage = aPageControl.currentPage;
    [UIView beginAnimations:nil context:NULL];
    [UIView setAnimationDuration:0.3f];
    [UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
    sv.contentOffset = CGPointMake(320.0f * whichPage, 0.0f);
    [UIView commitAnimations];
}

- (void) scrollViewDidScroll: (UIScrollView *) aScrollView
{
    CGPoint offset = aScrollView.contentOffset;
    pageControl.currentPage = offset.x / 320.0f;
}

- (void) viewDidLoad
{
    self.navigationController.navigationBar.tintColor = COOKBOOK_PURPLE_COLOR;
    self.title = @"Image Scroller";
    
    // Create the scroll view and set its content size and delegate
    sv = [[[UIScrollView alloc] initWithFrame:CGRectMake(0.0f, 0.0f, 320.0f, BASEHEIGHT)] autorelease];
    sv.contentSize = CGSizeMake(NPAGES * 320.0f, sv.frame.size.height);
    sv.pagingEnabled = YES;
    sv.delegate = self;
    
    int i = 0;
    // Load in all the pages
    for ( i;i < NPAGES; i++)
    {
        NSString *filename = [NSString stringWithFormat:@"image%d.png", i+1];
        UIImageView *iv = [[UIImageView alloc] initWithImage:[UIImage imageNamed:filename]];
        iv.frame = CGRectMake(i * 320.0f, 0.0f, 320.0f, BASEHEIGHT);
        [sv addSubview:iv];
        [iv release];
    }
    
    [self.view addSubview:sv];
    
    pageControl.numberOfPages = 3;
    pageControl.currentPage = 0;
    [pageControl addTarget:self action:@selector(pageTurn:) forControlEvents:UIControlEventValueChanged];
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,以下是一个使用 Objective-C 纯代码编写的轮播图示例: 1. 创建一个 UIScrollView 对象和一个 UIPageControl 对象: ```objective-c @interface ViewController () <UIScrollViewDelegate> @property (nonatomic, strong) UIScrollView *scrollView; @property (nonatomic, strong) UIPageControl *pageControl; @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; // 初始化 scrollView self.scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, 200)]; self.scrollView.delegate = self; self.scrollView.pagingEnabled = YES; self.scrollView.showsHorizontalScrollIndicator = NO; [self.view addSubview:self.scrollView]; // 初始化 pageControl self.pageControl = [[UIPageControl alloc] initWithFrame:CGRectMake(0, 180, self.view.bounds.size.width, 20)]; self.pageControl.currentPageIndicatorTintColor = [UIColor redColor]; self.pageControl.pageIndicatorTintColor = [UIColor lightGrayColor]; [self.view addSubview:self.pageControl]; } @end ``` 2. 在 scrollView 中添加图片: ```objective-c - (void)viewDidLayoutSubviews { [super viewDidLayoutSubviews]; // 添加图片到 scrollView 中 NSArray *imageNames = @[@"image1", @"image2", @"image3", @"image4"]; CGFloat width = self.scrollView.bounds.size.width; CGFloat height = self.scrollView.bounds.size.height; for (int i = 0; i < imageNames.count; i++) { UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(i * width, 0, width, height)]; imageView.image = [UIImage imageNamed:imageNames[i]]; [self.scrollView addSubview:imageView]; } // 设置 scrollView 的 contentSize self.scrollView.contentSize = CGSizeMake(width * imageNames.count, height); } ``` 3. 实现 scrollView 的滚动和 pageControl 的更新: ```objective-c - (void)scrollViewDidScroll:(UIScrollView *)scrollView { // 更新 pageControl 的当前页码 NSInteger currentPage = (NSInteger)(scrollView.contentOffset.x / scrollView.bounds.size.width + 0.5); self.pageControl.currentPage = currentPage; } ``` 4. 最后,为了让轮播图自动滚动,可以使用定时器来实现: ```objective-c - (void)viewDidAppear:(BOOL)animated { [super viewDidAppear:animated]; // 定时器自动滚动 NSTimer *timer = [NSTimer scheduledTimerWithTimeInterval:2.0 target:self selector:@selector(nextPage) userInfo:nil repeats:YES]; [[NSRunLoop currentRunLoop] addTimer:timer forMode:NSRunLoopCommonModes]; } - (void)nextPage { // 计算下一页的页码 NSInteger nextPage = (self.pageControl.currentPage + 1) % self.pageControl.numberOfPages; CGPoint offset = CGPointMake(nextPage * self.scrollView.bounds.size.width, 0); [self.scrollView setContentOffset:offset animated:YES]; } ``` 以上就是一个简单的使用 Objective-C 纯代码编写的轮播图示例。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值