码迷,mamicode.com
首页 > 其他好文 > 详细

(5.19)scrollview的分页滚动

时间:2014-05-20 08:49:13      阅读:303      评论:0      收藏:0      [点我收藏+]

标签:分页控件   style   c   tar   color   a   


#import "mainController.h"

@interface mainController ()
//分页控件
@property(nonatomic,weak)UIPageControl *pageController;
//滚动视图
@property(nonatomic,weak)UIScrollView *scrollder;

@end

@implementation mainController
//开发步骤
//1,搭建ui界面,建立scrollview
//2,增加分页功能
//3,添加分页控件


-(void)loadView{

    //1,实例化视图view
    self.view = [[UIView alloc]initWithFrame:[UIScreen mainScreen].applicationFrame];
    
    //2,创建滚动视图
    UIScrollView *scroll = [[UIScrollView alloc]initWithFrame:self.view.bounds];
    //3,将scroll加载到当前视图
    [self.view addSubview:scroll];

    //4,初始化数据
    NSMutableArray *array = [[NSMutableArray alloc]initWithCapacity:5];
    for (NSInteger i = 0; i < 5; i++) {
        
    //创建图片的文件名
        NSString *imageName = [NSString stringWithFormat:@"%d.jpg",i+1];
    //建立图像视图
        UIImage *images = [UIImage imageNamed:imageName];
        
        UIImageView *imageView = [[UIImageView alloc]initWithImage:images];
        
    //将视图添加到数组
        [array addObject:imageView];
        
        CGFloat width = scroll.bounds.size.width;
        //将建立好的数组添加到scrollView
        [array enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
            //1,从数组中取出imageView
            UIImageView *imageView = obj;
            //2,设置图像视图的frame
            [imageView setFrame:CGRectMake(width * idx, 0, width, scroll.bounds.size.height)];
            //3,将图像视图添加到scroller
            [scroll addSubview:imageView];
            
        }];
        
        
        //设置滚动视图属性
        //1,设置滚动区域
        [scroll setContentSize:CGSizeMake(scroll.bounds.size.width * 5, scroll.bounds.size.width)];
        //2,允许分页
        [scroll setPagingEnabled:YES];
        //3,关闭弹簧效果
        [scroll setBounces:NO];
        //4,关闭水平滚动条
        [scroll setShowsHorizontalScrollIndicator:NO];
        
        //设置滚动视图代理
        [scroll setDelegate:self];
        
        
        //添加分页控件
        UIPageControl *pageControl = [[UIPageControl alloc]init];
        //根据指定的页数,返回分页控件的大小
        CGSize size = [pageControl sizeForNumberOfPages:5];
        [pageControl setFrame:CGRectMake(0, 0, size.width, size.height)];
        //设置分页控件的位子
        [pageControl setCenter:CGPointMake(width / 2 , 400)];
        //设置分页控件的页数
        [pageControl setNumberOfPages:5];
        [pageControl setCurrentPage:0];
        //设置分页控件的颜色
        [pageControl setCurrentPageIndicatorTintColor:[UIColor redColor]];
        [pageControl setPageIndicatorTintColor:[UIColor blackColor]];
        
        //添加到视图
        [self.view addSubview:pageControl];
        
        self.pageController =pageControl;
        self.scrollder = scroll;
        
        
        //添加分页空间的监听方法
        [pageControl addTarget:self action:@selector(pageChange:) forControlEvents:UIControlEventValueChanged];
        
        
    }

}

#pragma mark - uiscrollview代理方法
#pragma mark 滚动停止事件
-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{

    NSLog(@"滚动停止");
    //根据scrollview的方法,判断页数
    NSInteger pageNo = scrollView.contentOffset.x / scrollView.bounds.size.width;
    [self.pageController setCurrentPage:pageNo];
}

#pragma 设置分页控件的监听方法
-(void)pageChange:(UIPageControl *)sender{

    NSLog(@"分页控件被点击");
    //判断当前的控件数
    CGFloat offsetX = sender.currentPage * self.scrollder.bounds.size.width;
    [self.scrollder setContentOffset:CGPointMake(offsetX, 0) animated:YES];


}




@end

(5.19)scrollview的分页滚动,布布扣,bubuko.com

(5.19)scrollview的分页滚动

标签:分页控件   style   c   tar   color   a   

原文地址:http://www.cnblogs.com/modingding/p/3737830.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!