【问题标题】:How do I add views to an UIScrollView and keep it synchronized with a UIPageControl?如何将视图添加到 UIScrollView 并使其与 UIPageControl 保持同步?
【发布时间】:2012-01-29 22:44:23
【问题描述】:

我需要在我的应用程序中动态创建一些视图并再次动态地在它们上面放置一些按钮。如果按钮的数量(计数)超过十个,我想将按钮放在新视图上,并且视图之间的转换必须使用 UIPageControl。即使我在 Apple 的开发者页面上搜索和搜索,我也找不到解决问题的方法。有人可以帮忙吗?

【问题讨论】:

  • UIPageControl Help 的可能重复项
  • 我不认为这是重复的,我看过那个问题,但我不想使用 UIScrollView。感谢警告

标签: objective-c ipad uiview uipagecontrol


【解决方案1】:

使用addSubview 方法将您的视图添加为 UIScrollView 的并排子视图。然后将 UIPageControl 与 UIScrollView 一起使用,如 example


我创建了一个管理 UIScrollView、UIPageControl 和 UIView 数组的类。这是我在自己的代码中使用的简化版本。它执行以下操作:

  • 设置滚动视图以显示 UIView 数组。它不关心视图是否是动态生成的。
  • 处理滚动和页面控制事件。
  • 将滚动视图与页面控件同步。

PageViewManager.h

#import <Foundation/Foundation.h>

@interface PageViewManager : NSObject <UIScrollViewDelegate>
{
    UIScrollView* scrollView_;
    UIPageControl* pageControl_;
    NSArray* pages_;
    BOOL pageControlUsed_;
    NSInteger pageIndex_;
}

- (id)initWithScrollView:(UIScrollView*)scrollView
             pageControl:(UIPageControl*)pageControl;
- (void)loadPages:(NSArray*)pages;
- (void)loadControllerViews:(NSArray*)pageControllers;

@end

PageViewManager.m

#import "PageViewManager.h"

@interface PageViewManager ()

- (void)pageControlChanged;

@end

@implementation PageViewManager

- (id)initWithScrollView:(UIScrollView*)scrollView
             pageControl:(UIPageControl*)pageControl
{
    self = [super init];
    if (self)
    {
        scrollView_ = scrollView;
        pageControl_ = pageControl;
        pageControlUsed_ = NO;
        pageIndex_ = 0;

        [pageControl_ addTarget:self action:@selector(pageControlChanged)
               forControlEvents:UIControlEventValueChanged];
    }
    return self;
}

/*  Setup the PageViewManager with an array of UIViews. */
- (void)loadPages:(NSArray*)pages
{
    pages_ = pages;
    scrollView_.delegate = self;
    pageControl_.numberOfPages = [pages count];

    CGFloat pageWidth  = scrollView_.frame.size.width;
    CGFloat pageHeight = scrollView_.frame.size.height;

    scrollView_.pagingEnabled = YES;
    scrollView_.contentSize = CGSizeMake(pageWidth*[pages_ count], pageHeight);
    scrollView_.scrollsToTop = NO;
    scrollView_.delaysContentTouches = NO;

    [pages_ enumerateObjectsUsingBlock:^(id obj, NSUInteger index, BOOL *stop)
    {
        UIView* page = obj;
        page.frame = CGRectMake(pageWidth * index, 0,
                                pageWidth, pageHeight);
        [scrollView_ addSubview:page];
    }];
}

/*  Setup the PageViewManager with an array of UIViewControllers. */
- (void)loadControllerViews:(NSArray*)pageControllers
{
    NSMutableArray* pages = [NSMutableArray arrayWithCapacity:
                             pageControllers.count];
    [pageControllers enumerateObjectsUsingBlock:
        ^(id obj, NSUInteger idx, BOOL *stop)
        {
            UIViewController* controller = obj;
            [pages addObject:controller.view];
        }];

    [self loadPages:pages];
}

- (void)pageControlChanged
{
    pageIndex_ = pageControl_.currentPage;

    // Set the boolean used when scrolls originate from the page control.
    pageControlUsed_ = YES;

    // Update the scroll view to the appropriate page
    CGFloat pageWidth  = scrollView_.frame.size.width;
    CGFloat pageHeight = scrollView_.frame.size.height;
    CGRect rect = CGRectMake(pageWidth * pageIndex_, 0, pageWidth, pageHeight);
    [scrollView_ scrollRectToVisible:rect animated:YES];
}

- (void)scrollViewDidScroll:(UIScrollView*)sender
{
    // If the scroll was initiated from the page control, do nothing.
    if (!pageControlUsed_)
    {
        /*  Switch the page control when more than 50% of the previous/next
            page is visible. */
        CGFloat pageWidth = scrollView_.frame.size.width;
        CGFloat xOffset = scrollView_.contentOffset.x;
        int index = floor((xOffset - pageWidth/2) / pageWidth) + 1;
        if (index != pageIndex_)
        {
            pageIndex_ = index;
            pageControl_.currentPage = index;
        }
    }
}

- (void)scrollViewWillBeginDragging:(UIScrollView*)scrollView
{
    pageControlUsed_ = NO;
}

- (void)scrollViewDidEndDecelerating:(UIScrollView*)scrollView
{
    pageControlUsed_ = NO;
}

@end

要使用此类,请将其嵌入到 UIViewController 中,而不是包含 UIScrollView 和 UIPageControl。

用法:

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.

    // Create some views dynamically
    UIView* v1 = ...
    UIView* v2 = ...

    // Put the views inside an NSArray:
    NSArray* pages_ = [NSArray arrayWithObjects:v1, v2, nil];

    /* Create the PageViewManager, which is a member (or property) of this
       UIViewController. The UIScrollView and UIPageControl belong to this 
       UIViewController, but we're letting the PageViewManager manage them for us. */
    pageViewManager_ = [[PageViewManager alloc]
                        initWithScrollView:self.scrollView
                               pageControl:self.pageControl];

    // Make the PageViewManager display our array of UIViews on the UIScrollView.
    [pageViewManager_ loadViews:pages_];
}

我的示例代码假设您使用的是 ARC。

【讨论】:

  • 对不起,我不能在没有滚动视图的情况下使用它吗?
  • 如果您希望能够使用手指从一个页面滚动到下一个页面(如 iPhone 的主屏幕),那么最简单的方法(据我所知)是使用 UIScrollView .当用户在您的用户界面上看到 UIPageControl 时,他们会期望能够通过轻弹从一个页面移动到另一个页面。
  • 谢谢@Emile,我正是在问这个问题。
  • 这个例子是静态页码,我想动态创建和增加新视图
  • 如果您不知道如何以编程方式创建视图,请在网络上搜索“以编程方式创建 UIView”,您会发现很多关于此主题的指南。有关 iPhone 开发的书籍也将详细介绍这一点。阅读并尝试后,如果遇到问题,您可以在此处发布新问题寻求帮助。
猜你喜欢
  • 1970-01-01
  • 2011-09-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-12-04
  • 2011-04-01
  • 2020-08-07
相关资源
最近更新 更多