iOS UI-UIScrollView控件实现图片轮播 (UIPageControl-分页指示器)
一、实现效果
实现图片的自动轮播

二、实现代码
storyboard中布局

代码:
#import "ViewController.h"
#define HMImageCount 5
@interface ViewController ()<UIScrollViewDelegate>
@property (weak, nonatomic) IBOutlet UIScrollView *scrollView;
@property (weak, nonatomic) IBOutlet UIPageControl *pageControl;
@property (nonatomic, strong) NSTimer *timer;
@end @implementation ViewController - (void)viewDidLoad {
[super viewDidLoad]; CGFloat imageW = self.scrollView.bounds.size.width;
CGFloat imageH = self.scrollView.bounds.size.height;
CGFloat imageY = ;
CGFloat imageX = ;
for (NSInteger i = ; i < HMImageCount; i++) {
UIImageView *imageView = [[UIImageView alloc] init];
// 拼接图片名称
NSString *imageName = [NSString stringWithFormat:@"img_%02ld", i + ];
imageView.image = [UIImage imageNamed:imageName];
// 计算图片的X
imageX = imageW * i;
imageView.frame = CGRectMake(imageX, imageY, imageW, imageH);
// 把每一张图片添加到scrollView中
[self.scrollView addSubview:imageView];
} // 1.设置scrollView的滚动范围
self.scrollView.contentSize = CGSizeMake(imageW * HMImageCount, );
// 2.设置分页
self.scrollView.pagingEnabled = YES;
// 3.隐藏水平滚动条
self.scrollView.showsHorizontalScrollIndicator = NO;
// 4.隐藏垂直滚动条
self.scrollView.showsVerticalScrollIndicator = NO; // 设置总页数
self.pageControl.numberOfPages = HMImageCount;
// 设置当前在第几页
self.pageControl.currentPage = ;
// 设置指示器在当前页的时候颜色
self.pageControl.currentPageIndicatorTintColor = [UIColor purpleColor];
// 默认的颜色
self.pageControl.pageIndicatorTintColor = [UIColor yellowColor]; // 5.设置srollView的代理
self.scrollView.delegate = self; // 6.添加定时器
[self timer]; } - (void)nextPage {
// 1.获取当前页码
NSInteger page = self.pageControl.currentPage;
// 2.判断是不是最后一页
if (page == HMImageCount - ) {
// 如果是最后一页就回到第0页
page = ;
} else {
// 如果不是最后一页
page++;
}
// 3.用每页的宽度 * (页码+1) == 计算除了下一页的contentoffset.x
CGFloat offsetX = page*self.scrollView.frame.size.width;
// 4.设置UIScrollView的contentoffset等于新的偏移值
[self.scrollView setContentOffset:CGPointMake(offsetX, ) animated:YES];
} // 实现scrollView滚动的方法
- (void)scrollViewDidScroll:(UIScrollView *)scrollView { //如何计算当前滚动到了第几页
// 1.获取滚动的x方向的偏移量
CGFloat offsetX = self.scrollView.contentOffset.x;
offsetX = offsetX + self.scrollView.frame.size.width/;
// 2.用x方向的偏移量除以一张图片的宽度,取商就是当前滚动到了第几页
int page = offsetX/self.scrollView.frame.size.width;
//3.将页码赋值给UIPageControl
self.pageControl.currentPage = page; } // 开始拖拽
- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView {
// 让定时器停止
[self.timer invalidate];
self.timer = nil;
} // 停止拖拽的时候开启定时器
- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate { [self timer];
} - (NSTimer *)timer {
if (_timer == nil) { // 通过scheduledTimerWithTimeInterval这个方法创建的计时器控件,创建好以后自动启动
_timer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(nextPage) userInfo:nil repeats:YES];
// 把当前的定时器添加到当前的运行循环中,并指定它为通用模式,这样主线程在执行的时候就可以抽那么一点时间来关注一下我们的定时器---修改NSTimer的优先级与其他控件一样
[[NSRunLoop currentRunLoop] addTimer:_timer forMode:NSRunLoopCommonModes];
}
return _timer;
}
@end
提示:以下两个属性已经和storyboard中的控件进行了连线。
@property (weak, nonatomic) IBOutlet UIScrollView *scrollview;
@property (weak, nonatomic) IBOutlet UIPageControl *pageControl;
补充:定时器NSTimer
定时器 适合用来隔一段时间做一些间隔比较长的操作
NSTimeInterval:多长多件操作一次
target :操作谁
selector : 要操作的方法
userInfo: 传递参数
repeats: 是否重复
self.timer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(nextImage) userInfo:nil repeats:YES];
iOS UI-UIScrollView控件实现图片轮播 (UIPageControl-分页指示器)的更多相关文章
- iOS开发UI篇—UIScrollView控件实现图片轮播
iOS开发UI篇—UIScrollView控件实现图片轮播 一.实现效果 实现图片的自动轮播 二.实现代码 storyboard中布局 代码: #import "YYV ...
- 【转】 iOS开发UI篇—UIScrollView控件实现图片轮播
原文:http://www.cnblogs.com/wendingding/p/3763527.html iOS开发UI篇—UIScrollView控件实现图片轮播 一.实现效果 实现图片的自动轮播 ...
- UIScrollView控件实现图片轮播
http://www.cnblogs.com/dyf520/p/3805297.html 一.实现效果 实现图片的自动轮播 二.实现代码 storyboard中布局 代码: 1 ...
- iOS开发UI篇—UIScrollView控件实现图片缩放功能
iOS开发UI篇—UIScrollView控件实现图片缩放功能 一.缩放 1.简单说明: 有些时候,我们可能要对某些内容进行手势缩放,如下图所示 UIScrollView不仅能滚动显示大量内容,还能对 ...
- iOS开发项目实战——Swift实现图片轮播与浏览
近期開始开发一个新的iOS应用,自己决定使用Swift.进行了几天之后,发现了一个非常严峻的问题.那就是无论是书籍,还是网络资源,关于Swift的实在是太少了,随便一搜全都是OC实现某某某功能.就算是 ...
- UIScrollView控件实现图片缩放功能
转发自:http://www.cnblogs.com/wendingding/p/3754268.html 一.缩放 1.简单说明: 有些时候,我们可能要对某些内容进行手势缩放,如下图所示 UIScr ...
- ios开发图片轮播器以及定时器小问题
一:图片轮播器效果如图:能实现自动轮播,到最后一页时,轮播回来,可以实现拖拽滚动 二:代码: #import "ViewController.h" ; @interface Vie ...
- 不同手机根据坐标计算控件、图片的像素,px 与 dp, sp换算公式?
参考该帖子:http://www.cnblogs.com/bluestorm/p/3640786.html PPI = Pixels per inch,每英寸上的像素数,即 "像素密度&qu ...
- iOS开发UI篇—UIScrollView控件介绍
iOS开发UI篇—UIScrollView控件介绍 一.知识点简单介绍 1.UIScrollView控件是什么? (1)移动设备的屏幕⼤大⼩小是极其有限的,因此直接展⽰示在⽤用户眼前的内容也相当有限 ...
随机推荐
- 04: python常用模块
目录: 1.1 时间模块time() 与 datetime() 1.2 random()模块 1.3 os模块 1.4 sys模块 1.5 tarfile用于将文件夹归档成 .tar的文件 1.6 s ...
- SVC(STM32)
这两个都是 system level service,有什么区别呢?…… 手册上说 SVC 这个指令是同步的,而 PendSV 是异步的,请问是什么意思呢?…… 高手路过尽请留言啊
- Educational Codeforces Round 21 Problem A - C
Problem A Lucky Year 题目传送门[here] 题目大意是说,只有一个数字非零的数是幸运的,给出一个数,求下一个幸运的数是多少. 这个幸运的数不是最高位的数字都是零,于是只跟最高位有 ...
- 一种新的技术,C++/CLI
一.来源 在一个项目中,拿到了一个demo,看起来像是C#,又像是C++,部分截图如下 1.界面[C#的winform] 2.mian入口,是cpp 3.解决方案 二.猜测 一开始以为是C#工程,因为 ...
- 51nod 1284 2 3 5 7的倍数
从1到N 里 是2的倍数 有 N/2 个 然后大概看过这类的blog 所以运用容斥原理 直接计算 是 2 3 5 7 的个数都是多少 然后用N 减去 就是 不是2 3 5 7 的个数了 (离散好像也 ...
- Elasticsearch工作原理
一.关于搜索引擎 各位知道,搜索程序一般由索引链及搜索组件组成. 索引链功能的实现需要按照几个独立的步骤依次完成:检索原始内容.根据原始内容来创建对应的文档.对创建的文档进行索引. 搜索组件用于接收用 ...
- Educational Codeforces Round 53 Editorial
After I read the solution to the problem, I found that my solution was simply unsightly. Solved 4 ou ...
- Educational Codeforces Round 1 E. Chocolate Bar dp
题目链接:http://codeforces.com/contest/598/problem/E E. Chocolate Bar time limit per test 2 seconds memo ...
- python 阶乘
product= i= : product=i*product print('i=%d' %i,end='') print('\tproduct=%d' %product) i+= print('\n ...
- MongoDB(课时5 数据查询)
3.4.2 数据查询 对于数据的查询操作核心语法: db.集合名称.find({查询条件}, {设置显示的字段}) 范例:没查询条件 db.info.find() 范例:有查询条件,查询出url为&q ...