IOS——使用ScrollView和PageController实现引导页分页效果

2023-05-16

如标题所说,使用的是ScrollView和PageControlle两个控件共同达到该效果

首先,使用storyboard将ScrollView和PageControlle拖入,要注意的是,PageControl不能被ScrollView包含在内,二者要分开属于平级关系

然后要在.h文件中添加UIScrollViewDelegate

然后在.m文件中实现下面代码

-(void) initUI{
    //init scrollView
    self.automaticallyAdjustsScrollViewInsets=NO;
    scrollView.backgroundColor=[UIColor groupTableViewBackgroundColor];
    scrollView.bounces=YES;
    scrollView.pagingEnabled=YES;
    scrollView.scrollEnabled=YES;
    scrollView.showsHorizontalScrollIndicator=YES;
    scrollView.showsVerticalScrollIndicator=YES;
    scrollView.pagingEnabled=YES;
    scrollView.delegate=self;
    
    NSArray *array=[NSArray arrayWithObjects:[UIColor orangeColor],[UIColor purpleColor], nil];
    for (int i=0; i<array.count; i++) {
        UIView *view=[[UIView alloc] initWithFrame:CGRectMake(i*scrollView.frame.size.width,0,scrollView.frame.size.width,scrollView.frame.size.height)];
        view.backgroundColor=[array objectAtIndex:i];
        [scrollView addSubview:view];
    }
    scrollView.contentSize=CGSizeMake(scrollView.frame.size.width*3, scrollView.frame.size.height);
    
    //init pageControll
    pageControll.backgroundColor=[UIColor groupTableViewBackgroundColor];
    pageControll.numberOfPages=array.count;
    pageControll.currentPage=0;
    
}
#pragma ScrollView
-(void) scrollViewDidScroll:(UIScrollView *)scrollView{
    NSLog(@"完成滚动");
    int index=fabs(scrollView.contentOffset.x)/scrollView.frame.size.width;
    pageControll.currentPage=index;
}

-(void) scrollViewWillBeginDragging:(UIScrollView *)scrollView{
    NSLog(@"将要开始滚动");
}

-(void) scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate{
    NSLog(@"结束滚动");
}

-(void) scrollViewWillBeginDecelerating:(UIScrollView *)scrollView{
    NSLog(@"滚动将要开始减速");
}

-(void) scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
    NSLog(@"滚动将要减速到停止");
}

-(BOOL) scrollViewShouldScrollToTop:(UIScrollView *)scrollView{
    return YES;
}

-(void) scrollViewDidScrollToTop:(UIScrollView *)scrollView{
    NSLog(@"滚动到顶部");
}
#pragma ScrollView end


本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

IOS——使用ScrollView和PageController实现引导页分页效果 的相关文章

随机推荐