iOS Paging Controls (Navigation)

一曲冷凌霜 提交于 2019-12-10 19:22:41

问题


Is there a way that I can use those paging dots independently in my app,

add them to my UIView/NIB and call the functions on it to set total number of dots or current page/dot.

Basically I've UIViewImage in a nib file, i'm displaying images (names taken off an array) on swipe gestures and want to show those paging dots on the bottom for the navigation information.


回答1:


UIPageControl has it's total page & current page/dot just as you described, let's say pageControl is a instance of UIPageControl, then you can initiate the numberOfPages & currentPage as below:

pageControl.numberOfPages = [images count];
pageControl.currentPage = 0;

Also, you can add action for it when the pageControl's dots was tapped. changePage: method here is just a example:

[pageControl addTarget:self action:@selector(changePage:) forControlEvents:UIControlEventValueChanged];

You can add your images to a UIScrollView and use its delegate method: scrollViewDidScroll::

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

It'll change the pageControl's current dot when your swiped to a new image.



来源:https://stackoverflow.com/questions/8475216/ios-paging-controls-navigation

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!