最后效果图:

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

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

最新文章

  1. 开着idea,死机了,关机重启。重启之后,重新打开idea报错java.lang.AssertionError:upexpected content storage modification
  2. [debian]SublimeText>PrettyCode無效
  3. PHP安装所最到的问题-解决方案
  4. iOS页面间传值的方式(NSUserDefault/Delegate/NSNotification/Block/单例)
  5. Oracle对表解锁的操作
  6. Erlang generic standard behaviours -- gen
  7. php异步调试和线上调试网站程序的方法
  8. Android ActionBar的Overlay模式如何不遮盖顶部内容的问题
  9. android 用ListView实现表格样式
  10. 手把手教你DIY一个春运迁徙图(一)
  11. 回调函数的意义以及python实现
  12. 让背景图片跟随div大小变化代码
  13. WS_EX_TOOLWINDOW 属性的陷阱
  14. 常见的jquery一些效果
  15. Ubuntu16.04卸载opencv2.4.9并安装opencv3.2.0+contrib
  16. 《CLR Via C#》学习--线程开销
  17. C++学习-7
  18. linux常用命令及使用技巧(一)
  19. JavaWeb案例:登陆和注册
  20. Eclipse安装TestNG插件

热门文章

  1. php中usort自定义排序如何使用
  2. Linux基本命令(二)
  3. oracle员工表和部门表基本操作
  4. C语言数据类型取值范围解析
  5. AndroidStudio如何配置NDK/JNI开发环境
  6. PHP移动互联网开发笔记(5)——基础函数库
  7. Android 开发者工具
  8. webcollector + selenium 爬取空间相册图片
  9. C# WPF 多个window 相互覆盖的次序控制 不用topmost
  10. C++调用IDL程序的做法(二)