iOS—UIPageControl的使用

 
  

//分页控件

    bluetoothPageControl = [UIPageControl new];

    [self.view addSubview:bluetoothPageControl];

    

    CGSize pageControlSize = [bluetoothPageControl sizeForNumberOfPages:self.blueToothPageCount];

    [bluetoothPageControl mas_makeConstraints:^(MASConstraintMaker *make) {

        make.size.mas_equalTo(CGSizeMake(pageControlSize.width, pageControlSize.height));

        make.centerX.equalTo(mainView);

        make.top.equalTo(bluetoothScrollView.mas_bottom).with.offset(0.5);

    }];

    

    bluetoothPageControl.numberOfPages = self.blueToothPageCount;//指定页面个数

    bluetoothPageControl.currentPage = 0;//默认选中的点(第一个)

    bluetoothPageControl.pageIndicatorTintColor = UIColorFromRGB(0xb3b3b3);//设置非选中页的圆点颜色

    bluetoothPageControl.currentPageIndicatorTintColor = UIColorFromRGB(0x39485c);//设置选中页的圆点颜色

    

    //给分页控件的小圆点设置点击事件

    [bluetoothPageControl addTarget:self action:@selector(clickPageControl:forEvent:) forControlEvents:UIControlEventValueChanged];

}

//点击分页控件的小圆点让ScrollView滚动到相应的页码

-(void)clickPageControl:(UIPageControl *)pageControl forEvent:(UIEvent *)event{

    

    [UIView animateWithDuration:0.5 animations:^{

        

        UITouch *touch = [[event allTouches] anyObject];

        if ([touch tapCount] == 1) {

            

            CGPoint point = [touch locationInView:pageControl];

            CGFloat centerX = point.x;

            NSInteger page = (NSInteger)(centerX / (bluetoothPageControl.frame.size.width/self.blueToothPageCount));

            

            [pageControl setCurrentPage:page];

            bluetoothScrollView.contentOffset = CGPointMake(pageControl.currentPage * self.myTableView.frame.size.width, 0);

            WPLog(@"点击了分页控件的第 %ld 个小圆点",(long)page+1);

        }

    }];

    

}

#pragma mark -

- (void)scrollViewDidScroll:(UIScrollView *)scrollView{

    //设置PageControl的小圆点的位置就是当前ScrollView滚动到的当前页

    CGPoint contentOffset = scrollView.contentOffset;

    NSInteger page = contentOffset.x / scrollView.frame.size.width;

    bluetoothPageControl.currentPage = page;

}

你可能感兴趣的:(iOS—UIPageControl的使用)