UIScrollview + UIPageControl 实现简单例子

#define   NewfeatureCount  4
@interface NewfeatureViewController ()<UIScrollViewDelegate>
@property(nonatomic,weak)  UIPageControl  *pageControl;
@end

- (void)viewDidLoad {
    [super viewDidLoad];

    //创建一个scrollview:显示所有的新特性
    UIScrollView  *scrollView = [[UIScrollView  alloc]init];
    scrollView.frame = self.view.bounds;
    [self.view  addSubview:scrollView];

    for (int  i = 0; i < NewfeatureCount; i++) {
        UIImageView  *image = [[UIImageView  alloc]init];
        image.size = scrollView.size;
        image.y = 0 ;
        image.x = i * image.width;
        //显示图片
        NSString  *name = [NSString  stringWithFormat:@"new_feature_%d",i+1];
        image.image = [UIImage  imageNamed:name];
        [scrollView  addSubview:image];
    }
    
    //如果想要某个方向不能滚动,那就就该方向上的值设置为0
    scrollView.contentSize = CGSizeMake(NewfeatureCount * scrollView.width, 0);
    
    scrollView.bounces = NO;//去除弹簧效果
    scrollView.pagingEnabled = YES;
    scrollView.showsHorizontalScrollIndicator = NO;
    scrollView.delegate = self;
    
    //添加pageControl:设置分页
    
    UIPageControl  *pageControl = [[UIPageControl  alloc]init];
    pageControl.numberOfPages = NewfeatureCount;
    pageControl.width = 100;
    pageControl.height = 50;
    //pageControl.backgroundColor = [UIColor  redColor];
    pageControl.centerX = scrollView.width * 0.5;
    pageControl.centerY = scrollView.height -50;
    pageControl.currentPageIndicatorTintColor = [UIColor  redColor];   
    pageControl.pageIndicatorTintColor = [UIColor  grayColor];
    pageControl.userInteractionEnabled = NO;//不许与用户界面交互
    [self.view  addSubview:pageControl];
    self.pageControl = pageControl;
    
}
-(void)scrollViewDidScroll:(UIScrollView *)scrollView{

    double  page = scrollView.contentOffset.x / scrollView.width;
    
    //四舍五入计算页面
    self.pageControl.currentPage = (int)(page + 0.5);
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值