我需要動態地在我的應用程序中創建一些視圖,並再次動態地放置一些按鈕。如果按鈕的數量(數量)多於十個,我希望將按鈕放在新視圖上,並且視圖之間的轉換必須與UIPageControl
一起。即使我從Apple的開發者頁面搜索並搜索,我也找不到解決我的問題的方法。有人可以幫忙嗎?如何將視圖添加到UIScrollView並使其與UIPageControl保持同步?
回答
使用addSubview
方法將您的視圖添加爲UIScrollView的並排子視圖。然後使用帶有UIScrollView的UIPageControl,如example。
我發管理一個UIScrollView,一個UIPageControl,和UIViews的陣列的一類。它是我在自己的代碼中使用的簡化版本。它執行以下操作:
- 設置滾動視圖以顯示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內比包含UIScro llView和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。
對不起,但我不能使用它沒有scrollview? – 2012-01-29 23:02:51
如果您希望能夠通過使用手指(如iPhone的主屏幕)從一個頁面滾動到下一個頁面,那麼最簡單的方法(我知道的)是使用UIScrollView。當用戶在用戶界面上看到UIPageControl時,他們希望能夠通過輕彈從一個頁面移動到另一個頁面。 – 2012-01-29 23:12:08
謝謝@Emile,我正是這樣問的。 – 2012-01-29 23:21:48
- 1. 如何將UIScrollview與UIPagecontrol結合使用以顯示不同的視圖?
- 2. UIScrollView作爲子視圖添加UIViewController?與UIPageControl
- 3. 如何在UIScrollView中添加UIPageControl
- 4. 如何添加多個視圖到UIScrollView
- 5. 將viewController視圖添加到UIScrollView
- 6. 將動態視圖添加到UIScrollView
- 7. 如何結合UIScrollview和UIPagecontrol顯示不同的視圖?
- 8. TFS:與其主人保持同步
- 9. 將視圖添加到UIScrollView並切換滾動/互動
- 10. 如何創建Uistackview並將其添加到視圖中?
- 11. Android:如何將視圖添加到WindowManager,並始終將其保持在應用程序的頂部?
- 12. 如何在UIScrollView上添加UIViewController視圖
- 13. 添加圖表視圖,並使其
- 14. 從URL加載圖像並將其添加到列表視圖
- 15. 如何將子視圖添加到其他視圖控制器?
- 16. 保持與公共git同步並同步到一個私人git與
- 17. 如何讓UIScrollView/UIPageControl無限?
- 18. UIScrollView與UITableViewCell中的UIPageControl
- 19. UIPageControl渲染與UIScrollView怪癖
- 20. 如何保持圖像同步?
- 21. 如何將視圖添加到sapui5中的路線圖步驟
- 22. 如何保持火力地堡同步與其他數據庫
- 23. 如何使用UIPageControl從一個視圖導航到其他視圖?
- 24. 添加視圖的UIScrollView
- 25. WPF - 將節點添加到樹視圖並將其保存爲XML
- 26. 添加一個子視圖到UIScrollView
- 27. 添加子視圖到UiScrollView ..順利
- 28. 如何保持不同步
- 29. angularjs:對視圖模型的中間表示並保持同步
- 30. 如何添加視圖到一個SQL同步
可能重複的[UIPageControl幫助](http://stackoverflow.com/questions/1816144/uipagecontrol-help) – 2012-01-29 22:56:25
我不認爲這是重複我看過那個問題,但我不想用UIScrollView的。感謝您的警告 – 2012-01-29 23:23:23