iOS_9_scrollView分页

最后效果图:



BeyondViewController.h

//
//  BeyondViewController.h
//  8_scrollVIew分页浏览
//
//  Created by beyond on 14-7-25.
//  Copyright (c) 2014年 com.beyond. All rights reserved.
//

#import <UIKit/UIKit.h>

@interface BeyondViewController : UIViewController
@property (weak, nonatomic) IBOutlet UIScrollView *scrollView;

@end



BeyondViewController.m

//
//  BeyondViewController.m
//  8_scrollVIew分页浏览
/*
    下面代码存在性能问题,仅作为新特性介绍界面使用
    不可作为图片浏览器~
    1,一次性生成8个ImageView会存在性能问题,解决方法:使用3个ImageView(或2个ImageView)
    2,另外,循环播放还没实现
 */
//  Created by beyond on 14-7-25.
//  Copyright (c) 2014年 com.beyond. All rights reserved.
//

#import "BeyondViewController.h"
// 图片总张数
#define kImgCount 8
@interface BeyondViewController ()<UIScrollViewDelegate>
{
    // 分页条码指示控制器
    UIPageControl *_pageControl;
}

@end

@implementation BeyondViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
	// 调用自己定义方法
    [self scrollViewWithPage];
}

// 带分页功能的scrollView
- (void)scrollViewWithPage
{
    
    // 1,设置scrollView的可视大小,内容大小,等属性
    _scrollView.frame = self.view.bounds;
    _scrollView.showsHorizontalScrollIndicator = NO;
    _scrollView.showsVerticalScrollIndicator = NO;
    _scrollView.bouncesZoom = NO;
    _scrollView.bounces = NO;
    // 设置代码,监听滚动完成的事件
    _scrollView.delegate = self;
    
    // 2,创建8个UIImageView,加入到scrollView
    // 每一个图片宽,高
    CGFloat imgW = self.view.bounds.size.width;
    CGFloat imgH = self.view.bounds.size.height;
    for (int i=0; i<kImgCount; i++) {
        // UIImageView
        // 图片名:01.jpg ~ 07.jpg
        NSString *imgName = [NSString stringWithFormat:@"0%d.png",i+1];
        UIImageView *imgView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:imgName]];
        // 假设保持imageView里面的image不变形
        //设置UIImageView的对象的下面两个属性,能够图片不变形且充满图片框为前提进行填充。
        imgView.clipsToBounds = YES;
        imgView.contentMode = UIViewContentModeScaleAspectFill;
        // y是0,x是一张连着一张
        imgView.frame = CGRectMake(i*imgW, 0, imgW, imgH);
       // 将全部的图片加入到scrollView
        [_scrollView addSubview:imgView];
    }
    
    // 3,这个最重要,是滚动区域
    // _scrollView.contentSize = CGSizeMake(kImgCount*imgW, imgH);
    // 0代表高度方向不滚动
    _scrollView.contentSize = CGSizeMake(kImgCount*imgW, 0);
    // 按scrollView的宽度分页
    _scrollView.pagingEnabled = YES;
    
    
    // 4,pageControl分页指示条
    _pageControl = [[UIPageControl alloc]init];
    // pageControl分页指示条的中心点在底部中间
    _pageControl.numberOfPages = kImgCount; //这个最重要
    _pageControl.center = CGPointMake(imgW*0.5, imgH-20);
    _pageControl.bounds = CGRectMake(0, 0, 150, 15);
    _pageControl.pageIndicatorTintColor = [UIColor grayColor];
    _pageControl.currentPageIndicatorTintColor = [UIColor redColor];
    _pageControl.enabled = NO; //取消其默认的点击行为
    [self.view addSubview:_pageControl];
    
}
/*
    在这种方法里面,能够进行性能优化,由于时时在监听滚动,从而随时进行3个UIImageView的拼接,甚至可精简到仅仅有2个UIImageView进行动态拼接
 */
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    // scrollView的contentOffset是最重要的属性,点,x,y记录的是滚动的距离,相对的是scrollView的可视界面的左上角的距离
    CGPoint offset = scrollView.contentOffset;

    int curPageNo = offset.x / _scrollView.bounds.size.width;
    _pageControl.currentPage = curPageNo ;
    
}


@end








版权声明:本文博客原创文章,博客,未经同意,不得转载。

原文地址:https://www.cnblogs.com/gcczhongduan/p/4642094.html