原文:http://www.cnblogs.com/wendingding/p/3763527.html

iOS开发UI篇—UIScrollView控件实现图片轮播

一、实现效果

实现图片的自动轮播

          

二、实现代码

storyboard中布局

代码:

 #import "YYViewController.h"

 @interface YYViewController () <UIScrollViewDelegate>
@property (weak, nonatomic) IBOutlet UIScrollView *scrollview;
/**
* 页码
*/
@property (weak, nonatomic) IBOutlet UIPageControl *pageControl; @property (nonatomic, strong) NSTimer *timer;
@end @implementation YYViewController - (void)viewDidLoad
{
[super viewDidLoad]; // 图片的宽
CGFloat imageW = self.scrollview.frame.size.width;
// CGFloat imageW = 300;
// 图片高
CGFloat imageH = self.scrollview.frame.size.height;
// 图片的Y
CGFloat imageY = ;
// 图片中数
NSInteger totalCount = ;
// 1.添加5张图片
for (int i = ; i < totalCount; i++) {
UIImageView *imageView = [[UIImageView alloc] init];
// 图片X
CGFloat imageX = i * imageW;
// 设置frame
imageView.frame = CGRectMake(imageX, imageY, imageW, imageH);
// 设置图片
NSString *name = [NSString stringWithFormat:@"img_0%d", i + ];
imageView.image = [UIImage imageNamed:name];
// 隐藏指示条
self.scrollview.showsHorizontalScrollIndicator = NO; [self.scrollview addSubview:imageView];
} // 2.设置scrollview的滚动范围
CGFloat contentW = totalCount *imageW;
//不允许在垂直方向上进行滚动
self.scrollview.contentSize = CGSizeMake(contentW, ); // 3.设置分页
self.scrollview.pagingEnabled = YES; // 4.监听scrollview的滚动
self.scrollview.delegate = self; [self addTimer];
} - (void)nextImage
{
int page = (int)self.pageControl.currentPage;
if (page == ) {
page = ;
}else
{
page++;
} // 滚动scrollview
CGFloat x = page * self.scrollview.frame.size.width;
self.scrollview.contentOffset = CGPointMake(x, );
} // scrollview滚动的时候调用
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
NSLog(@"滚动中");
// 计算页码
// 页码 = (contentoffset.x + scrollView一半宽度)/scrollView宽度
CGFloat scrollviewW = scrollView.frame.size.width;
CGFloat x = scrollView.contentOffset.x;
int page = (x + scrollviewW / ) / scrollviewW;
self.pageControl.currentPage = page;
} // 开始拖拽的时候调用
- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView
{
// 关闭定时器(注意点; 定时器一旦被关闭,无法再开启)
// [self.timer invalidate];
[self removeTimer];
} - (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate
{
// 开启定时器
[self addTimer];
} /**
* 开启定时器
*/
- (void)addTimer{ self.timer = [NSTimer scheduledTimerWithTimeInterval: target:self selector:@selector(nextImage) userInfo:nil repeats:YES];
}
/**
* 关闭定时器
*/
- (void)removeTimer
{
[self.timer invalidate];
}
@end
补充:
1.先介绍下UIScrollView的常见属性                    

 @property(nonatomic) CGPoint contentOffset; // 记录UIScrollView滚动的位置
@property(nonatomic) CGSize contentSize; // 内容尺寸(能滚动的范围)
@property(nonatomic) UIEdgeInsets contentInset; // 额外增加的滚动区域(在上下左右4个边缘)
@property(nonatomic,assign) id<UIScrollViewDelegate> delegate; // 代理对象
@property(nonatomic) BOOL bounces; // 是否有弹簧效果
@property(nonatomic) BOOL showsHorizontalScrollIndicator; // 是否显示水平滚动条
@property(nonatomic) BOOL showsVerticalScrollIndicator; // 是否显示垂直滚动条


2.分页浏览的实现                            

2.1.把需要显示的图片设置进UIScrollView 
 
 CGFloat w = self.view.frame.size.width;
CGFloat h = self.view.frame.size.height;
for (int i = ; i< kCount; i++) {
UIImageView *imageView = [[UIImageView alloc] init]; // 1.设置frame
imageView.frame = CGRectMake(i * w, , w, h); // 2.设置图片
NSString *imgName = [NSString stringWithFormat:@"0%d.jpg", i + ];
imageView.image = [UIImage imageNamed:imgName]; [_scrollView addSubview:imageView];

2.2.设置UIScrollView的相关属性                            

属性在文章的开头有介绍

 // height == 0 代表 禁止垂直方向滚动
_scrollView.contentSize = CGSizeMake(kCount * w, );
_scrollView.showsHorizontalScrollIndicator = NO;
_scrollView.pagingEnabled = YES;
_scrollView.delegate = self;

2.3.设置UIPageControl的相关属性,计算页码                            

 UIPageControl *pageControl = [[UIPageControl alloc] init];
pageControl.center = CGPointMake(w * 0.5, h - );
pageControl.bounds = CGRectMake(, , , );
pageControl.numberOfPages = kCount; // 一共显示多少个圆点(多少页)
// 设置非选中页的圆点颜色
pageControl.pageIndicatorTintColor = [UIColor redColor];
// 设置选中页的圆点颜色
pageControl.currentPageIndicatorTintColor = [UIColor blueColor]; // 禁止默认的点击功能
pageControl.enabled = NO; [self.view addSubview:pageControl];
_pageControl = pageControl;

2.4.滚动时切换页码                                         

 #pragma mark - UIScrollView的代理方法
#pragma mark 当scrollView正在滚动的时候调用
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
int page = scrollView.contentOffset.x / scrollView.frame.size.width;
// NSLog(@"%d", page); // 设置页码
_pageControl.currentPage = page;
}

                           

【转】 iOS开发UI篇—UIScrollView控件实现图片轮播的更多相关文章

  1. iOS开发UI篇—UIScrollView控件实现图片轮播

    iOS开发UI篇—UIScrollView控件实现图片轮播 一.实现效果 实现图片的自动轮播            二.实现代码 storyboard中布局 代码: #import "YYV ...

  2. iOS开发UI篇—UIScrollView控件实现图片缩放功能

    iOS开发UI篇—UIScrollView控件实现图片缩放功能 一.缩放 1.简单说明: 有些时候,我们可能要对某些内容进行手势缩放,如下图所示 UIScrollView不仅能滚动显示大量内容,还能对 ...

  3. iOS开发UI篇—UIScrollView控件介绍

    iOS开发UI篇—UIScrollView控件介绍 一.知识点简单介绍 1.UIScrollView控件是什么? (1)移动设备的屏幕⼤大⼩小是极其有限的,因此直接展⽰示在⽤用户眼前的内容也相当有限 ...

  4. iOS开发UI篇—UITableview控件简单介绍

    iOS开发UI篇—UITableview控件简单介绍 一.基本介绍 在众多移动应⽤用中,能看到各式各样的表格数据 . 在iOS中,要实现表格数据展示,最常用的做法就是使用UITableView,UIT ...

  5. iOS开发UI篇—UITableview控件基本使用

    iOS开发UI篇—UITableview控件基本使用 一.一个简单的英雄展示程序 NJHero.h文件代码(字典转模型) #import <Foundation/Foundation.h> ...

  6. iOS开发UI篇—UITableview控件使用小结

    iOS开发UI篇—UITableview控件使用小结 一.UITableview的使用步骤 UITableview的使用就只有简单的三个步骤: 1.告诉一共有多少组数据 方法:- (NSInteger ...

  7. UIScrollView控件实现图片轮播

    http://www.cnblogs.com/dyf520/p/3805297.html 一.实现效果 实现图片的自动轮播            二.实现代码 storyboard中布局 代码: 1 ...

  8. iOS开发-UI (一)常用控件

    从这里开始是UI篇 知识点: 1.常用IOS基本控件 2.UITouch ======================= 常用基本控件 1.UISegmentedControl:分段控制器 1)创建方 ...

  9. iOS UI-UIScrollView控件实现图片轮播 (UIPageControl-分页指示器)

    一.实现效果 实现图片的自动轮播            二.实现代码 storyboard中布局 代码: #import "ViewController.h" #define HM ...

随机推荐

  1. Git创建一个自己的本地仓库

    如果我们要把一个项目加入到Git的版本管理中,可以在项目所在的目录用git init命令建立一个空的本地仓库,然后再用git add命令把它们都加入到Git本地仓库的暂存区(stage or inde ...

  2. bzoj3437

    练一下斜率优化 ..] of int64; q,a,b:..] of longint; i,n,h,t,j:longint; function g(j,k:longint):double; var a ...

  3. HDU --- 4006

    The kth great number Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65768/65768 K (Java/Oth ...

  4. Linux学习笔记12——Unix中的进程

    通过调用fork和exec函数都能创建新的进程,但两者有着本质的区别:fork函数拷贝了父进程的内存映像,而exec函数用用新的映像来覆盖调用进程的进程映像的功能. 一  fork函数 #includ ...

  5. 关于Unity的ViewSpace(CameraSpace)的坐标系

    从昨天开始遇到一个看似很小,但令我苦恼的问题,由于对Unity的坐标系没有直接搞清楚,所以导致一个shader没看懂,于是发了个贴:http://game.ceeger.com/forum/read. ...

  6. Git error: hint: Updates were rejected because the remote contains work that you do hint: not have locally. This is usually caused b

    hint: Updates were rejected because the remote contains work that you dohint: not have locally. This ...

  7. if语句,if...else if语句和switch...case语句的区别和分析

    前段时间在工作中遇到了一个关于条件判断语句的问题,在if语句,if else if语句和switch case语句这三者之间分析,使用其中最有效率的一种方法. 所以就将这个问题作为自己第一篇博客的主要 ...

  8. 爬虫技术浅析 | WooYun知识库

    爬虫技术浅析 | WooYun知识库 爬虫技术浅析 好房通ERP | 房产中介软件最高水准领导者 undefined

  9. mysql 导出,导入数据

     导出     加-d代表只导出表结构 命令行下具体用法如下:  mysqldump -u用户名 -p密码 -d 數據库名 表名 脚本名; 1.导出数据库为dbname的表结构(其中用户名为root, ...

  10. 正则表达式start(),end(),group()方法

    一.捕获组的概念 捕获组可以通过从左到右计算其开括号来编号,编号是从1 开始的.例如,在表达式 ((A)(B(C)))中,存在四个这样的组: 1     ((A)(B(C))) 2     (A) 3 ...