iOS基础-UIScrollView、UIPageControl

UIScrollView的常用属性

UIScrollview主要专长于两个方面:

滚动:contentSize大于Frame.size的时候,能够滚动。

缩放:自带缩放,可以指定缩放倍数。

滚动属性

contentSize:定义内容区域大小,决定是否能够滑动

contentOffset:视图左上角距离坐标原点的偏移量

scrollsToTop:滑动到顶部(点击状态条的时候)

pagingEnabled:是否整屏翻动

bounces:边界是否回弹

scrollEnabled:是否能够滚动

showsHorizontalScrollIndicator:控制是否显示水平方向的滚动条

showVerticalScrollIndicator:控制是否显示垂直方向的滚动条

alwaysBounceVertical:控制垂直方向遇到遇到边框是否反弹

alwaysBounceHorizontal:控制水平方向遇到边框是否反弹

缩放属性

minimumZoomScale:缩小的最小比例

maximumZoomScale:放大的最大比例

zoomScale:设置变化比例

zooming:判断是否正在进行缩放反弹

bouncesZoom:控制缩放的时候是否会反弹

注:要实现缩放,还要实现delegate,指定缩放的视图是谁

UIScrollViewDelegate

- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
    //滚动就会触发
}

- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView{
    //开始拖拽时触发
}

- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate{
    //结束拖拽是触发
}

- (void)scrollViewWillBeginDecelerating:(UIScrollView *)scrollView{
    //开始减速时触发
}

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
    //结束减速是触发(停下的时候)
}

- (void)scrollViewDidEndZooming:(UIScrollView *)scrollView withView:(UIView *)view atScale:(CGFloat)scale{
    //完成放大缩小触发
}

- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView{
    //指定某个UIScrollview的子视图可以被放大缩小
}

 UIPageControl

常用属性

currentPage:当前页

numberOfPages:指定页面的个数

注意:事件触发使用是UIControlEventsValueChanged

 

pageControl与scrollView合用

通常在Scrollview滚动的时候修改pageControl的currentPage

使用场景:引导页,首页的轮播图

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor grayColor];
    _scrView = [[UIScrollView alloc] initWithFrame:[UIScreen mainScreen].bounds];
    //创建图片
     _x = [UIScreen mainScreen].bounds.size.width;
     _w = [UIScreen mainScreen].bounds.size.width;
     _h = [UIScreen mainScreen].bounds.size.height;
    for (int i = 1; i < 5; i++) {
        NSString *str = [NSString stringWithFormat:@"%d.png",i];
        UIImage *image = [UIImage imageNamed:str];

        
        UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake((i-1) * _x, 0, _w, _h)];
        imageView.image = image;
        [self.scrView addSubview:imageView];
        [imageView release];
    }
    
//    UIImageView *image1 = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"6"]];
//    image1.tag = 100;
    [self.view addSubview:_scrView];
//    [_scrView addSubview:image1];
    
    //设置scr滚动范围
    _scrView.contentSize = CGSizeMake(_x * 4, 1200);
    self.scrView.pagingEnabled = YES;
    self.scrView.delegate = self;
    [self.scrView release];
//    self.scrView.maximumZoomScale = 2.0;
//    self.scrView.minimumZoomScale = 0.5;
    
    _pageCtr = [[UIPageControl alloc] initWithFrame:CGRectMake(160, 10, 100, 20)];
    _pageCtr.numberOfPages = 4;
    _pageCtr.currentPage = 0;
//    _pageCtr.backgroundColor = [UIColor redColor];
    _pageCtr.pageIndicatorTintColor = [UIColor redColor];
    _pageCtr.currentPageIndicatorTintColor = [UIColor greenColor];
    [self.view addSubview:_pageCtr];
    
    UIButton *btn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    btn.frame = CGRectMake(300, 20, 60, 30);
    btn.layer.borderWidth = 1;
    btn.layer.cornerRadius = 5;
    
    [btn setTitle:@"下一页" forState:UIControlStateNormal];
    [btn addTarget:self action:@selector(btnClick) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:btn];
    
    [NSTimer scheduledTimerWithTimeInterval:2.0 target:self selector:@selector(nextImage) userInfo:nil repeats:YES];
    [_pageCtr addTarget:self action:@selector(nextImage) forControlEvents:UIControlEventValueChanged];
    
    
}

- (void)btnClick{
    SecondViewController *svc = [[SecondViewController alloc] init];
    [self presentViewController:svc animated:YES completion:nil];
}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
    NSLog(@"开始拖了");
    int page = (scrollView.contentOffset.x + scrollView.frame.size.width * 0.5)/ scrollView.frame.size.width;
    self.pageCtr.currentPage = page;
}


- (void)nextImage{
    if (self.pageCtr.currentPage == 3) {
        self.pageCtr.currentPage =0;
        
    }else{
        self.pageCtr.currentPage++;
    }
    CGFloat offsetX = self.pageCtr.currentPage * self.scrView.frame.size.width;
    CGPoint offset = CGPointMake(offsetX, 0);
    [self.scrView setContentOffset:offset animated:YES];
}

 

posted @ 2015-09-26 16:22  dingjianjaja  阅读(180)  评论(0编辑  收藏  举报