使用scrollview和pageconrol?

时间:2014-06-17 21:12:10

标签: ios uiscrollview swift uipagecontrol

Swift 中,我创建了一个滚动视图,它可以正常工作。它是水平的,它是一个“教程”页面。对象出现在它上面很好。滚动很好。它有页面。但是,我希望页面在UIPageControl上显示当前页面。这是我的整个代码。目前,页面控件仅在我在视图上的页面控件上滑动某个方向时才有效。然后改变它。它移动屏幕上的对象。滚动视图会滚动,但不会更改UIPageControl。

代码:

var tutorialScrollView = UIScrollView()
var pageCounterForScrollView = UIPageControl()
tutorialScrollView.pagingEnabled = true
tutorialScrollView.bounces = true
tutorialScrollView.frame = CGRectMake(0, 0, 320, 568)
tutorialScrollView.userInteractionEnabled = true
tutorialScrollView.contentSize = CGSizeMake(1280, 568)
tutorialScrollView.showsHorizontalScrollIndicator = false

pageCounterForScrollView.numberOfPages = 3
pageCounterForScrollView.frame = CGRectMake(0, 0, 500, 100)
pageCounterForScrollView.center = CGPointMake(160, 528)
self.view.addSubview(scrollViewBGView)
self.view.addSubview(tutorialScrollView)
self.view.addSubview(pageCounterForScrollView)
self.tutorialScrollView.addSubview(reactFastSVLabel)
self.tutorialScrollView.addSubview(changingDotSVView)
self.tutorialScrollView.addSubview(learnPlaySVLabel)
self.tutorialScrollView.addSubview(simpleSVLabel)
self.tutorialScrollView.addSubview(oneColorSVImageView)
self.tutorialScrollView.addSubview(justDoThisSVLabel)

func scrollViewDidEndDecelerating(scrollView: UIScrollView) {
    var newOffset: CGFloat = tutorialScrollView.contentOffset.x
    var newPage = Int(newOffset/tutorialScrollView.frame.size.width)
    pageCounterForScrollView.currentPage = newPage
}

1 个答案:

答案 0 :(得分:0)

我没有在swift中这样做,但我已经在Objective-C

中完成了
- (void)scrollViewDidScroll:(UIScrollView *)aScrollView
{
CGFloat pageWidth = self.horizontalMenu.bounds.size.width ;
     float fractionalPage = self.horizontalMenu.contentOffset.x / pageWidth ;
NSInteger nearestNumber = lround(fractionalPage) ;

if (self.pageControl.currentPage != nearestNumber)
{
    self.pageControl.currentPage = nearestNumber ;
    NSLog(@"%d",nearestNumber);
    // if we are dragging, we want to update the page control directly during the drag
    if (self.horizontalMenu.dragging)
        [self.pageControl updateCurrentPageDisplay] ;
}
}