- Java 双重比较
- java - 比较器与 Apache BeanComparator
- Objective-C 完成 block 导致额外的方法调用?
- database - RESTful URI 是否应该公开数据库主键?
我需要在我的应用程序中动态创建一些 View 并再次动态地在它们上面放置一些按钮。如果按钮的数量(计数)超过十个,我想将按钮放在新 View 上,并且 View 之间的转换必须使用 UIPageControl
。尽管我在 Apple 的开发者页面上进行了谷歌搜索,但我找不到解决问题的方法。有人可以帮忙吗?
最佳答案
使用 addSubview
方法将您的 View 添加为 UIScrollView 的并排 subview 。然后将 UIPageControl 与 UIScrollView 一起使用,如 example 中所示.
我创建了一个管理 UIScrollView、UIPageControl 和 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
要使用此类,您将其嵌入到包含 UIScrollView 和 UIPageControl 的 UIViewController 中。
用法:
- (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。
关于objective-c - 如何向 UIScrollView 添加 View 并使其与 UIPageControl 保持同步?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9057462/
我使用本教程使用 UIPageController 创建了一个应用程序:http://www.appcoda.com/uipageviewcontroller-storyboard-tutorial/
我一直在使用 UIPageController 在不同的 Controller 之间导航。现在我想在底部添加这些点,这样用户就可以看到他在哪个 Controller 上。这是通过 UIPageCont
我是 ios 编程的新手。我试图在 stackoverflow 上搜索另一个示例和更多问题,但这不是我的目标。我想在 UIPageControl 的索引 0 处设置一个类似于 iPhone 搜索主
任何人都知道如何创建垂直 UIPageControl?我有一个启用了分页的 UIScrollView,我想让 UIPageControl 坐在侧面并垂直显示而不是水平显示。 先感谢您... 最佳答案
我想要带有圆圈的页面控件如何执行此操作我现在使用页面控件,但它有点而不是圆圈 最佳答案 您必须创建自定义页面滚动。为此,您应该引用 this question 。希望你能明白。 还有一些有用的链接:
嘿伙计们,我正在使用带有 ScrollView 的 UIPagecontrol。它效果很好,但有一种情况是白色没有突出显示。它发生在我们从 ScrollView 转到另一个 View 然后返回并重新加
我想隐藏UIPageControl的栏,因为我想实现带有滑动手势功能的分页,所以我不需要显示点栏。如果我设置 hidden=YES ,将会保留一个 UIPageControl 大小的黑色 block
我无法在 iOS 14 中设置页面控制点的边框颜色。以下代码适用于 iOS < 14,但不适用于 iOS 14: for (int i = 0; i < [self.subViews count];
pageControl = [[UIPageControl alloc] initWithFrame:CGRectMake(153,356,38,36) ]; pageControl.userInte
我有一个 PageViewController我想更改 UIPageControl位置(从屏幕底部到顶部)。 那是我的代码: UIPageControl *pageControl = [UIPageC
我有一个 UIPageViewController,其中放置了一个 UIPageControl。当我滚动时,这似乎在大多数情况下都有效,但有时它不会更改当前选定的页面。什么会触发这种行为? func
我有一个关于 UIPageControl 的问题。我有一个 UIScrollView 和一个 UIPageControl。当我在 UIScrollView 前面添加 UIPageControl 时,状
我使用默认的UIPageControl。 UIPageViewController 以编程方式创建。 dataSource 和 delegate 方法也已正确配置。 我正在尝试实现与 iOS 主页相同
我正在尝试使用页面控制功能和 UIScrollView 在我的 IOS 应用程序上显示一组图像,我看过一些教程,但它们都展示了如何更改颜色、如何加载一些图像以及使用页面控件来浏览它们? 最佳答案 -(
我正在使用 UIPageControl 和 UISwipeGestureRecognizer 在 View Controller 之间切换。使用滑动更改页面。 我还需要在其中一个 View Contr
我发现自定义UIPageControl页面图像非常容易(我已经检查过iOS7/8): [self setValue:[UIImage imageNamed:@"my_icon_for_off_stat
我需要改进 EAIntroView 屏幕在启用画外音时从我的应用程序的转换。为此,我考虑使用变换来增加触摸区域: _pageControl.transform = CGAffineTransf
我有一个包含多个表格的 UIScrollView 和一个跟踪用户当前正在查看哪个页面的 UIPageControl。它工作正常,直到我开始在 tableview 上向上滚动,此时 UIPageCont
我正在使用 UIPageController 来实现基于页面的导航应用程序。我想仅在页面边缘启用滑动手势,并防止在内部内容 View 中启用滑动手势。该页面的边距为 20 像素: contentVie
我想知道如何替换状态栏或至少将页面控件放在它上面?每次有人寻呼时,这将是一个临时放置几秒钟,然后它就会消失。我以前在应用程序中见过它。 最佳答案 我认为您无法到达状态栏上方,因为它不属于您的根窗口。
我是一名优秀的程序员,十分优秀!