2012-12-13 6 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

質問は何です:あなたは、「左」と「右」のiOS自体があなただけの正しいフレームにスクロールできるように、タップドットた実現しているため、スクロールの世話をする必要はありませんか?私は何が問題なのか理解していませんでしたか? –

+0

@pranjal私pagecontrolのドットをクリックしてページを移動したい –

+0

リンクを介して行くhttp://www.edumobile.org/iphone/iphone-programming-tutorials/pagecontrol-example-in-iphone/ –

答えて

59
Pagecontrolの値変更イベントにバインドし

- (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]; 
} 

あなたは一つのイベントの変更ページを作成することができます。

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

バンカーミタルの回答を完了するだけで、簡単に行うには、希望の矩形をスクロールして表示するのが一番簡単です。

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) 

    } 
関連する問題