2012-12-13 17 views
35

這裏點的頁面我有一個很好的工作,但在點擊點不改變頁面,請在changepage的功能幫助的PageControl:我怎樣才能改變一下的UIPageControl

- (void)viewDidLoad { 
    scrollView=[[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, 320, 420)]; 
    scrollView.delegate = self; 
    [self.scrollView setBackgroundColor:[UIColor whiteColor]]; 
    [scrollView setCanCancelContentTouches:NO]; 
    scrollView.indicatorStyle = UIScrollViewIndicatorStyleWhite; 
    scrollView.clipsToBounds = YES; 
    scrollView.scrollEnabled = YES; 
    [scrollView setShowsHorizontalScrollIndicator:NO]; 
    scrollView.pagingEnabled = YES; 
    [self.view addSubview:scrollView]; 
    pageControl=[[UIPageControl alloc]initWithFrame:CGRectMake(0, 420, 320, 40)]; 
    [pageControl addTarget:self action:@selector(changepage:) forControlEvents:UIControlEventTouchUpInside]; 
    [self.view addSubview:pageControl]; 

    UIView *blueView = [[UIView alloc] init]; 
    blueView.frame = CGRectMake(0, 0, 640, 480); 
    blueView.backgroundColor = [UIColor whiteColor]; 
    [scrollView addSubview:blueView]; 
    self.pageControl.numberOfPages = 2; 
    [scrollView setContentSize:CGSizeMake(640, 0)]; 
} 

-(void)changepage:(id)sender 
{ 
    int page = pageControl.currentPage; 
    if (page < 0) 
     return; 
    if (page >= 2) 
     return; 
    CGRect frame = scrollView.frame; 
    frame.origin.x = frame.size.width * page; 
    frame.origin.y = 0; 
    [scrollView scrollRectToVisible:frame animated:YES]; 

} 

- (void)scrollViewDidScroll:(UIScrollView *)_scrollView 
{ 
    if (pageControlIsChangingPage) 
     return; 
    CGFloat pageWidth = _scrollView.frame.size.width; 
    int page = floor((_scrollView.contentOffset.x - pageWidth/2)/pageWidth) + 1; 
    pageControl.currentPage = page; 
} 
+0

問題是什麼?我不明白是什麼問題? –

+0

@pranjal先生我想點擊頁面控制點移動頁面 –

+0

通過鏈接http://www.edumobile.org/iphone/iphone-programming-tutorials/pagecontrol-example-in-iphone/ –

回答

59

您可以創建一個事件更改頁面:

- (IBAction)changePage:(id)sender { 
    UIPageControl *pager=sender; 
    int page = pager.currentPage; 
    CGRect frame = imageGrid_scrollView.frame; 
    frame.origin.x = frame.size.width * page; 
    frame.origin.y = 0; 
    [imageGrid_scrollView scrollRectToVisible:frame animated:YES]; 
} 

將其綁定到Pagecontrol的值更改事件。

2

這是一個斯威夫特2解決方案UICollectionView。 單擊UIPageControl's dots可將當前視圖向左或向右移動。

// Define bounds 
private var currentIndex:Int = 0 
private let maxLimit = 25 
private let minLimit = 0 

// Define @IBAction from UIPageControl 
@IBAction func moveViewLeftRight(sender: UIPageControl) { 
    // Move to Right 
    if currentIndex < maxLimit && sender.currentPage > currentIndex { 
     currentIndex++ 
     self.collectionView.scrollToItemAtIndexPath(NSIndexPath(forItem: currentIndex, inSection: 0), atScrollPosition: .Right, animated: true) 
    // Move to Left 
    } else if currentIndex > minLimit { 
     currentIndex-- 
     self.collectionView.scrollToItemAtIndexPath(NSIndexPath(forItem: currentIndex, inSection: 0), atScrollPosition: .Left, animated: true) 
    } 
} 
+0

擁有scrollRectToVisible也更容易。但是如果你想滾動到你的CollectionView中的一個項目,你可以通過sender.currentPage。系統將挖掘點用作當前頁面。 – Fry

4

只是爲了完成Banker Mittal的回答,最簡單的方法就是將想要的矩形滾動到可見狀態。你不必照顧「左」和「右」滾動,因爲iOS本身意識到你點擊了哪個點,所以你可以滾動到正確的框架:

private func moveScrollViewToCurrentPage() { 

//You can use your UICollectionView variable too instead of my scrollview variable 
      self.scrollView 
       .scrollRectToVisible(CGRect(
        x: Int(self.scrollView.frame.size.width) * self.pager.currentPage, 
        y: 0, 
        width:Int(self.scrollView.frame.size.width), 
        height: Int(self.scrollView.frame.size.height)), 
            animated: true) 

    }