UIPageControl点击时只翻一页解决办法

UIPageControl使亮点直接跳到点击dot上

  公司要求做这样的一个功能,要求点某一个白点的时候,直接跳到对应的页面,但是苹果默认只跳一页,无论你点击哪个白点,以前一直没有做过pageControl,网上也没有找到相关的解决方法,于是乎自己琢磨了一下,其实也挺简单,其实所谓的dot就是加在pageControl上的UIImageView,有两种状态,一种是正常态,一种是高亮状态,而这些dot默认的userInteractionEnabled = NO;

所以解决办法也很简单了,把每个dot循环出来,将其userInteractionEnabled设为YES,并加上UIButton,将button的tag标注出来,并给button绑定一个事件,这样点击每个button也能取到其tag,取到tag就取到了对应的UIImageView,然后将button的tag赋给pageControl.currentPage,这样目的就达到了


pageControl = [[UIPageControl alloc] initWithFrame:CGRectMake(50, 430, 220, 30)];

    pageControl.center = CGPointMake(mainScreenWidth / 2, 430);

    pageControl.backgroundColor = [UIColor clearColor];

    pageControl.numberOfPages = 8;

    int i = 0;

    for (UIImageView *imgV in [pageControl subviews]) {

        

        imgV.userInteractionEnabled = YES;   //默认为NO

        UIButton *botButton = [UIButton buttonWithType:UIButtonTypeCustom];

        botButton.frame = imgV.bounds;

        botButton.tag = i;

        botButton.backgroundColor = [UIColor clearColor];

        [botButton addTarget:self action:@selector(tapBotAction:) forControlEvents:UIControlEventTouchUpInside];

        [imgV addSubview:botButton];

        i++;

    }

    //[pageControl.layer setCornerRadius:8];   //设置圆角

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

    [self.view addSubview:pageControl];

}


- (void)tapBotAction:(id)sender{

    

    int index = [(UIButton *)sender tag];

    pageControl.currentPage = index;

    [catalogScrollView setContentOffset:CGPointMake(index * catalogSlideRemoving, 0) animated:YES]; //if animated is 'NO' animat don't implement


你可能感兴趣的:(UIPageControl点击时只翻一页解决办法)