2016-02-28 86 views
1

我似乎有一些問題讓UIPageControl工作。Swift:從ScrollView更新UIPageControl

我有一個ViewController持有ScrollView。這個ScrollView加載可以被滑動的nib文件。見圖片:

enter image description here

下面是加載這些代碼:

self.addChildViewController(vc0) 
self.displayReportView.addSubview(vc0.view) 
vc0.didMoveToParentViewController(self) 

var frame1 = vc1.view.frame 
frame1.origin.x = self.view.frame.size.width 
vc1.view.frame = frame1 
self.addChildViewController(vc1) 
self.displayReportView.addSubview(vc1.view) 
vc1.didMoveToParentViewController(self) 

// And so on 
... 

在他們正確等滾動這工作得很好..

現在,在視圖控制器(一個保持滾動視圖)我添加了代表:

UIScrollViewDelegate 

創建一些變量:

var frame: CGRect = CGRectMake(0, 0, 0, 0) 
var colors:[UIColor] = [UIColor.redColor(), UIColor.blueColor(), UIColor.greenColor(), UIColor.yellowColor()] 
var pageControl : UIPageControl = UIPageControl(frame: CGRectMake(50, 300, 200, 20)) 

我說是需要的一些功能:

func configurePageControl() { 
    // The total number of pages that are available is based on how many available colors we have. 

    self.pageControl.numberOfPages = 4 
    self.pageControl.currentPage = 0 
    self.pageControl.tintColor = UIColor.redColor() 
    self.pageControl.pageIndicatorTintColor = UIColor.blackColor() 
    self.pageControl.currentPageIndicatorTintColor = UIColor.greenColor() 
    self.view.addSubview(pageControl) 

} 


// MARK : TO CHANGE WHILE CLICKING ON PAGE CONTROL 
func changePage(sender: AnyObject) ->() { 
    let x = CGFloat(pageControl.currentPage) * displayReportView.frame.size.width 
    displayReportView.setContentOffset(CGPointMake(x, 0), animated: true) 
} 


func scrollViewDidEndDecelerating(scrollView: UIScrollView) { 

    let pageNumber = round(scrollView.contentOffset.x/scrollView.frame.size.width) 
    pageControl.currentPage = Int(pageNumber) 
} 

現在,當我運行的應用程序滾動視圖點顯示,但是當我刷卡,他們不更新。

問題

如何更新點以反映視圖顯示的內容?

讓我知道你是否需要我的代碼中的其他東西來查看功能。

回答

6

你當然可以做你描述,如果您有分頁滾動視圖;我有一個使用此代碼它的一個例子:

func scrollViewDidEndDecelerating(scrollView: UIScrollView) { 
    let x = scrollView.contentOffset.x 
    let w = scrollView.bounds.size.width 
    pageControl.currentPage = Int(x/w) 
} 

除了您round,看起來有很多代碼,這讓我覺得你的代碼應該工作。這讓我覺得別的東西只是錯誤配置。 這是一個分頁滾動視圖嗎?您是否記得使這個對象成爲滾動視圖的delegate?使用日誌記錄或斷點來確保您的scrollViewDidEndDecelerating甚至被首先調用。

但是,我只想指出,您所描述的配置實際上是UIPageViewController免費提供給您的配置 - 具有視圖控制器視圖的滾動視圖以及頁面控件 - 因此您可能想使用它。

+0

這是一個工作可下載的頁面水平滾動視圖加上頁面控制的例子,所以你可能想自己嘗試一下,然後看看你自己的代碼不同:https://github.com/mattneub/Programming-iOS- Book-Examples/blob/80551757899141e786b7f601f3879a7b450923c2/bk2ch07p372paging/ch20p676paging/ViewController.swift – matt

+0

嗨馬特,謝謝你的回覆。我正在努力爭取這項工作仍在繼續,我對我的聖殿的小小虛榮即將破滅。模糊...我已經在這裏上傳了這個項目:https://jumpshare.com/v/2Lq5U0KW2ExBWezcw7o3 - 你認爲你可以看看並給我一些指示嗎? (得到膝蓋,祈禱和乞求) – JamesG

+0

抱歉,如果超級厚臉皮要求您查看該項目。澄清我試圖得到它,以便當用戶向左或向右滾動頁面控件將相應地更新。 – JamesG

0

我會用UICollectionView替換滾動視圖。這樣你就可以免費分頁,而且會更好,因爲分頁功能可以直接使用,無需計算幀偏移量。

一定要設置collectionView.pagingEnabled = true

爲了得到當前頁碼,做collectionView.indexPathsForVisibleItems().first?.item

改變頁面:

collectionView.scrollToItemAtIndexPath(newIndexPath, atScrollPosition: CenteredHorizontally, animated: true)

+0

我寧願找到一種方法來處理我所得到的。感謝您的建議,但會考慮爲下一個應用程序。 :) – JamesG