簡體   English   中英

以編程方式使用分頁導航到 scrollView 中的特定頁面

[英]Navigate to specific page in scrollView with paging, programmatically

如何以編程方式導航到特定頁面。 基本上我有一個帶有滾動視圖的應用程序,其中填充了一堆子視圖(在這種情況下是 tableViews)。 單擊子視圖時,它會放大並且用戶可以編輯和導航表格,但是當我縮小時,我會重新加載整個視圖,以防用戶進行任何更改。 當然,重新加載視圖會將用戶發送回頁面 0。我嘗試設置 pageControl.currentpage 屬性,但所做的只是更改 pageControl 的點。 這是否意味着出現問題,還是我還需要做其他事情?

所有控制頁面滾動的是這個方法:

- (void)scrollViewDidScroll:(UIScrollView *)sender 
{
CGFloat pageWidth = self.scrollView.frame.size.width;
int page = floor((self.scrollView.contentOffset.x - pageWidth / 2) / pageWidth) + 1;
self.pageControl.currentPage = page;

NSString *listName = [self.wishLists objectAtIndex:self.pageControl.currentPage];
self.labelListName.text = listName;
}

您必須手動計算相應的滾動位置。 滾動到第 i 頁:

[scrollView setContentOffset:CGPointMake(scrollView.frame.size.width*i, 0.0f) animated:YES];

Ole Begemann 的上述答案的 Swift 5 版本

scrollView.setContentOffset( CGPoint(x: scrollView.frame.size.width * i, y: 0.0), animated: true)

Swift 5你可以輕松使用scrollView.scrollRectToVisible

如果實現了Horizontal分頁滾動視圖,它具有view width of the device默認view width of the device的大小並希望導航到第n頁:

scrollView.scrollRectToVisible(CGRect(x: self.view.center.x * n, y: self.view.center.y, width: self.view.frame.width, height: self.view.frame.height) ,animated: true)

您只需要根據您的滾動視圖配置定義 CGRect() 位置。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM