我正在製作一個應用程序,我需要使用UIPageControl滾動瀏覽一些圖像。可悲的是,我不知道如何使用UIPageControl或UIScrollView。通過Apple的YouTube視頻或Xcode文檔鏈接將非常感謝!如何在我的應用程序中使用UIPageControl?
在此先感謝!
我正在製作一個應用程序,我需要使用UIPageControl滾動瀏覽一些圖像。可悲的是,我不知道如何使用UIPageControl或UIScrollView。通過Apple的YouTube視頻或Xcode文檔鏈接將非常感謝!如何在我的應用程序中使用UIPageControl?
在此先感謝!
這裏是一個代碼,這將有助於你
CGSize size = [[UIScreen mainScreen] bounds].size;
CGFloat frameX = size.width;
CGFloat frameY = size.height-30; //padding for UIpageControl
UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(offsetX, offsetY, frameX, frameY)];
scrollView.pagingEnabled = YES;
scrollView.backgroundColor = [UIColor clearColor];
scrollView.contentSize = CGSizeMake(frameX, frameY);
[self.view addSubview: scrollView];
//我們說你有圖像的陣列中imageArray
for(int i = 0; i < [imageArray]; i++)
{
UIImage *image = [UIImage imageNamed:[imageArray objectAtIndex:i]];
imageView = [[UIImageView alloc] initWithImage:image];
imageView.frame = CGRectMake(frameX * i, 0.0, frameX, frameY);
[scrollView addSubview:imageView];
}
scrollView.contentSize = CGSizeMake(frameX*[imageArray count], frameY);
請根據使用範圍申報的變量。我已經宣佈當地所有的變量,這樣就可能無法獲得混淆視聽,還添加UIPageControl在你的ViewController
的底部實施的UIScrollView的委託方法在當前頁面指示符
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
width = scrollView.frame.size.width;
float xPos = scrollView.contentOffset.x+10;
//Calculate the page we are on based on x coordinate position and width of scroll view
pageControl.currentPage = (int)xPos/width;
}
其中的PageControl是UIPageControl加在底部你的ViewController
@CppiLove:請不,我還沒有編譯的代碼。會出現語法錯誤,但我的意圖是解釋如何使用UIScrollView和UIPagecontrol – Rocker
這是一個非常好的教程由雷Wenderlich團隊如何建立尋呼滾動視圖:How To Use UIScrollView to Scroll and Zoom Content
蘋果DOCUME ntation還有一個如何實現分頁滾動視圖的示例:PhotoScroller
My answer到this previous question也可能對您有用。
滾動型:
滾動型被用於顯示多數意見/對象。我們可以通過水平或垂直滾動顯示這些視圖。 您可以處理縮放,平移,滾動等
你可以通過一些教程 Are there any good UIScrollView Tutorials on the net?
示例代碼了滾動:
UIScrollView *scroll = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0,self.view.frame.size.width, self.view.frame.size.height)];
//This will create a scrollview of device screen frame
您可以啓用滾動
scroll.scrollEnabled = YES;
實施例,用於將3次到滾動型
NSInteger numberOfViews = 3;
for (int i = 0; i < numberOfViews; i++) {
CGFloat xOrigin = i * self.view.frame.size.width;
UIView *awesomeView = [[UIView alloc] initWithFrame:CGRectMake(xOrigin, 0, self.view.frame.size.width, self.view.frame.size.height)];
awesomeView.backgroundColor = [UIColor colorWithRed:0.5/i green:0.5 blue:0.5 alpha:1];
[scroll addSubview:awesomeView];
[awesomeView release];
}
// 3 views added horizontally to the scrollview by using xOrigin.
最重要的部分在此爲是瞭解xOrigin。這將把每個UIView放在前一個UIView停止的位置,換句話說,每個UIView將在前一個UIView的末尾開始。 現在我們獲得了3個視圖的滾動視圖,並且可以水平添加。
設置的UIScrollView contentSize
scroll.contentSize = CGSizeMake(self.view.frame.size.width * numberOfViews, self.view.frame.size.height);
的contentSize是三個UIViews的寬度的簡單相加,如果每個UIView的寬度爲320,和我們有三個UIViews,您的contentSize寬度爲920 。
[self.view addSubview:scroll];
[scroll release];
//Add scrollview to viewcontroller
頁面控制: A頁數控制呈現一組表示頁水平點的用戶。當前頁面呈現爲白點。用戶可以從當前頁面轉到下一頁面或前一頁面。
要啓用分頁,您需要添加
scroll.pagingEnabled = YES;
pageControl = [[UIPageControl alloc] init]; //SET a property of UIPageControl
pageControl.frame = CGRectMake(100,self.view.frame.size.height-100,self.view.frame.size.width-200,100);
pageControl.numberOfPages = 3; //as we added 3 diff views
pageControl.currentPage = 0;
添加委託方法滾動型的實施的PageControl點滾動時
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
CGFloat pageWidth = self.scrollView.frame.size.width;
int page = floor((self.scrollView.contentOffset.x – pageWidth/2)/pageWidth) + 1; //this provide you the page number
pageControl.currentPage = page;// this displays the white dot as current page
}
現在你可以看到的PageControl了滾動。希望你能理解。請參閱本太 https://developer.apple.com/library/ios/documentation/uikit/reference/UIPageControl_Class/Reference/Reference.html
@Cintu的答案是正確的,但是'pageControl'會給頁面設置帶來壞的動畫,最好是在'scrollViewDidEndDecelerating'中設置'currentPage' – Nazir
https://developer.apple.com/library/ios/documentation/uikit/reference/UIPageControl_Class/Reference/Reference.html – Kevin