UIScrollView、UIPageControl、UISlider协同作业

创建UIScrollView及添加tableView
//UIScrollView能滚动就是改变了自身的bounds

//UIScrollView代理协议UIScrollViewDelegate
UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:self.view.bounds];
    scrollView.backgroundColor = [UIColor yellowColor];
    scrollView.tag = scrollViewTag;
    [self.view addSubview:scrollView];
    //设置滚动范围,滚动的前提:滚动范围要比scrollView本身的宽高要大
    scrollView.contentSize = CGSizeMake(kScreenWidth * 4, 0);
    
    for (int i = 1; i < 5; i++) {
        UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake((i - 1) * kScreenWidth, 0, kScreenWidth, SCREEN_HEIGHT)];
        
        UIImage *image = [UIImage imageNamed:[NSString stringWithFormat:@"%d", i]];
        imageView.image = image;
        imageView.tag = 1110 + i;
        UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 700, kScreenWidth, 20)];
        label.text = [NSString stringWithFormat:@"第%d页", i];
        [imageView addSubview:label];
        
        [scrollView addSubview:imageView];
        
    }
    //最重要的属性:设置偏移量,显示第几张照片
    //偏移量  scrollView滚动的时候,scrollVIew自身是固定不动的,动的是子视图。偏移量指的是子视图相对于scrollView发生的偏移量
//    scrollView.contentOffset = CGPointMake(kScreenWidth * 1, 0);
    //代理
    scrollView.delegate = self;
    //整页滚动
    scrollView.pagingEnabled = YES;
    //滚动条的显示与否
    //横向
    scrollView.showsHorizontalScrollIndicator = NO;
    //竖向
    scrollView.showsVerticalScrollIndicator = NO;
    //点击状态栏返回顶部
    scrollView.scrollsToTop = YES;
    //scrollView发的回弹效果
//    scrollView.bounces = NO;
    //能否滚动, 默认是YES
    scrollView.scrollEnabled = YES;

创建UIPageControl

UIPageControl *pageControl = [[UIPageControl alloc] initWithFrame:CGRectMake(0, 650, kScreenWidth, 50)];
    pageControl.numberOfPages = 4;
    pageControl.tag = pageControlTag;
//    pageControl.backgroundColor = [UIColor redColor];
    [pageControl addTarget:self action:@selector(pageControlAction:) forControlEvents:UIControlEventValueChanged];
    pageControl.pageIndicatorTintColor = [UIColor purpleColor];
    pageControl.currentPageIndicatorTintColor = [UIColor greenColor];
    
    [self.view addSubview:pageControl];

创建UISlider

UISlider *slider = [[UISlider alloc] initWithFrame:CGRectMake(0, 580, kScreenWidth, 20)];
    [self.view addSubview:slider];
    slider.minimumTrackTintColor = [UIColor greenColor];
    slider.maximumTrackTintColor = [UIColor redColor];
    slider.tag = sliderTag;
    slider.maximumValue = kScreenWidth * 3;
    [slider addTarget:self action:@selector(sliderAction:) forControlEvents:UIControlEventValueChanged];

UISlider回调方法

- (void)sliderAction:(UISlider *)slider{
    UIScrollView *scrollView = (UIScrollView *)[self.view viewWithTag:scrollViewTag];
    [scrollView setContentOffset:CGPointMake(slider.value, 0) animated:YES];
}

UIPageControl回调方法

- (void)pageControlAction:(UIPageControl *)pageControl{
    UIScrollView *scrollView = (UIScrollView *)[self.view viewWithTag:scrollViewTag];
    [scrollView setContentOffset:CGPointMake(pageControl.currentPage * kScreenWidth, 0) animated:YES];
}

UIScrollView代理方法
滚动方法

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

- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
    NSLog(@"已经滚动");
    NSLog(@"%@", NSStringFromCGPoint(scrollView.contentOffset));
    UIPageControl *pageControl = (UIPageControl *)[self.view viewWithTag:pageControlTag];
    pageControl.currentPage = (scrollView.contentOffset.x + kScreenWidth / 2 ) / kScreenWidth;
}

- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate{
    NSLog(@"停止拖拽");
}

- (void)scrollViewWillBeginDecelerating:(UIScrollView *)scrollView{
    
    NSLog(@"将要开始减速");
    
//    UIPageControl *pageControl = (UIPageControl *)[self.view viewWithTag:pageControlTag];
//    UISlider *slider = (UISlider *)[self.view viewWithTag:sliderTag];
//    slider.value = scrollView.contentOffset.x / kScreenWidth;
}

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
    NSLog(@"减速停止,停止滚动");
    
    UIPageControl *pageControl = (UIPageControl *)[self.view viewWithTag:pageControlTag];
    pageControl.currentPage = scrollView.contentOffset.x / kScreenWidth;
    UISlider *slider = (UISlider *)[self.view viewWithTag:sliderTag];
    slider.value = scrollView.contentOffset.x;
}

方法缩小方法

//指定scrollView上的哪一个子视图进行缩放
- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView{
    
    return [scrollView.subviews firstObject];
}

- (void)scrollViewDidZoom:(UIScrollView *)scrollView{
    
//    UIImageView *imageView = (UIImageView *)[scrollView.subviews firstObject];
//    imageView.center = self.view.center;
    
}
- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
    //只有缩小时才执行固定中心点的代码
    if (scrollView.zoomScale < 1) {
        NSLog(@"niaho");
        scrollView.contentOffset = CGPointMake((scrollView.contentSize.width - scrollView.frame.size.width) / 2, (scrollView.contentSize.height - scrollView.frame.size.height) / 2);
    }
}
//恢复原状
//    self.scrolView.zoomScale = 1;

你可能感兴趣的:(UIScrollView、UIPageControl、UISlider协同作业)