ios 学习 广告图片轮播器
//
// ViewController.m
// 图片轮播器
//
// Created by zjj on 15/5/23.
// Copyright (c) 2015年 zjj. All rights reserved.
// #import "ViewController.h"
#define kCount 5
@interface ViewController () <UIScrollViewDelegate>
@property (nonatomic,strong)UIScrollView *scroView;
@property (nonatomic,strong)UIPageControl *pageControl;
@property (nonatomic,strong)NSTimer *time;
@end @implementation ViewController
/**
开发思路
1>创建scrollView
2>用UIimageView加载图片 并修改图片的x值
3>创建分页控件UIpageControl
4>绑定分页圆点点 点击圆点点换下一页
5>绑定时钟
*/
#pragma mark 初始化scrollView
- (UIScrollView *)scroView
{
if (_scroView == nil) {
_scroView = [[UIScrollView alloc]initWithFrame:CGRectMake(, , , )];
_scroView.backgroundColor = [UIColor blueColor];
[self.view addSubview:_scroView];
_scroView.bounces = NO;
_scroView.showsHorizontalScrollIndicator = NO;
_scroView.showsVerticalScrollIndicator = NO;
_scroView.contentSize = CGSizeMake(_scroView.bounds.size.width * kCount, );
self.scroView.pagingEnabled = YES;
_scroView.delegate = self;
}
return _scroView ;
}
#pragma mark 初始化PageCOntrol
- (UIPageControl *)pageControl
{
if (_pageControl == nil) {
// 分页控件 跟scrollView 是独立的
_pageControl = [[UIPageControl alloc]init];
_pageControl.numberOfPages = kCount;
CGSize size = [_pageControl sizeForNumberOfPages:kCount];
_pageControl.bounds = CGRectMake(, , size.width, size.height);
_pageControl.center = CGPointMake(self.scroView.center.x, self.scroView.center.y+(self.scroView.center.y/));
_pageControl.pageIndicatorTintColor = [UIColor redColor];
_pageControl.currentPageIndicatorTintColor = [UIColor blackColor];
[self.view addSubview:_pageControl];
self.pageControl = _pageControl;
}
return _pageControl;
} - (void)viewDidLoad {
[super viewDidLoad];
for (int i = ; i < kCount; i++) {
NSString *icon = [NSString stringWithFormat:@"img_%02d",i + ];
UIImageView *imgView = [[UIImageView alloc]initWithFrame:self.scroView.bounds];
imgView.image = [UIImage imageNamed:icon];
[self.scroView addSubview:imgView];
}
// 添加事件之后执行该块代码重新赋值x值
[self.scroView.subviews enumerateObjectsUsingBlock:^(UIImageView *imageView, NSUInteger idx, BOOL *stop) {
CGRect frame = imageView.frame;
frame.origin.x = idx * frame.size.width;
imageView.frame = frame;
}];
self.pageControl.currentPage = ;
[self.pageControl addTarget:self action:@selector(pageChange:) forControlEvents:UIControlEventTouchUpInside];
[self start];
}
#pragma mark 启动时钟
- (void)start
{
// self.time = [NSTimer scheduledTimerWithTimeInterval:2.0 target:self selector:@selector(changeTime) userInfo:nil repeats:YES];// 按住屏幕 画面会停止轮播但 时钟会累积 连跳好几页
self.time = [NSTimer timerWithTimeInterval:2.0 target:self selector:@selector(changeTime) userInfo:nil repeats:YES];// 按住屏幕时钟 画面仍然不会停止轮播
[[NSRunLoop currentRunLoop] addTimer:self.time forMode:NSRunLoopCommonModes];// 添加到(运行循环)轮循
} - (void)changeTime
{
int page = (self.pageControl.currentPage + ) % kCount;
self.pageControl.currentPage = page;
[self pageChange:self.pageControl];
} #pragma mark 对scrollView进行操作 根据图片偏移位置计算页数
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
// NSLog(@"%@,%f", NSStringFromCGPoint(_scroView.contentOffset),_scroView.bounds.size.width);//取出scrollview中当前页的x
int page = _scroView.contentOffset.x / _scroView.bounds.size.width;
self.pageControl.currentPage = page; //当前页数
} - (void)pageChange :(UIPageControl *)page
{
CGFloat x = page.currentPage * self.scroView.bounds.size.width;
[self.scroView setContentOffset:CGPointMake(x, )animated:YES];
}
#pragma mark 拖动图片时停止时钟
- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView
{
[self.time invalidate];
}
#pragma mark 松手后再次启动时钟
- (void)scrollViewWillEndDragging:(UIScrollView *)scrollView withVelocity:(CGPoint)velocity targetContentOffset:(inout CGPoint *)targetContentOffset
{
[self start];
}
@end
ios 学习 广告图片轮播器的更多相关文章
- ios之无限图片轮播器的实现
首先实现思路:整个collectionView中只有2个cell.中间始终显示第二个cell. 滚动:向前滚动当前cell的脚标为0,向后滚动当前的cell脚标为2.利用当前cell的脚标减去1,得到 ...
- UIScrollView实现图片轮播器及其无限循环效果
图片轮播器: 一.实现效果 实现图片的自动轮播 二.实现代码 storyboard中布局 代码: 1 #import "YYViewController.h" ...
- IOS第六天(3:scrollView 图片轮播器)
IOS第六天(3:scrollView 图片轮播器) #import "HMViewController.h" #define kImageCount 5 @interface H ...
- ios之无限 自动 图片轮播器的实现
比较之前发布的手动无限图片轮播器进行了改进.实现了自动无限轮播的功能.比较适合团购标题分类下面的轮播器功能. 实现思路: * 开启一个定时器,把操作放入消息循环池.每隔一定时间,操作执行一次. * 注 ...
- iOS 简易无限滚动的图片轮播器-SDCycleScrollView
@interface ViewController () <</span>SDCycleScrollViewDelegate> @end @implementation Vie ...
- ios开发图片轮播器以及定时器小问题
一:图片轮播器效果如图:能实现自动轮播,到最后一页时,轮播回来,可以实现拖拽滚动 二:代码: #import "ViewController.h" ; @interface Vie ...
- UIScrollView实现图片轮播器的无限滚动
简介 在现在的一些App中常常见到图片轮播器,一般用于展示广告.新闻等数据,在iOS内并没有现成的控件直接实现这种功能,但是通过UIScrollView的允许分页设置,可以实现滚动轮播的功能. 轮播原 ...
- JavaScript图片轮播器
先贴上html的代码 <div class="ImgDiv"> <div class="Imgs" id="imgScroll&qu ...
- 推荐一款超级漂亮的HTML5 CSS3的图片轮播器
最近在学习HTML5和CSS3,印象最深的是CSS3的动画功能,不仅有浏览器原生支持,执行效率高,而且免去在js中自己管理timer. 本来想写一个图片轮播器练练手,结果在网上发现一个国人写的开源的图 ...
随机推荐
- 使用Python获取Linux系统的各种信息
哪个Python版本? 当我提及Python,所指的就是CPython 2(准确的是2.7).我会显式提醒那些相同的代码在CPython 3 (3.3)上是不工作的,以及提供一份解释不同之处的备选代码 ...
- drush cc all 报错
请看好 指明了Module文件的行数 报错一定要多看看哦.
- wampserver2.5 apache2.4.9:forbidden,本机可以访问,局域网内部能访问。
wampserver2.5 apache2.4.9:forbidden,本机可以访问,局域网内部能访问. 因为做项目,多人分工,需要局域网内访问各自的项目. 然后安装了wampserver2.5,Ap ...
- virtualbox虚拟机上安装centOS的网络配置(安装centos时选择桥接网络)
最近接触hadoop,需要在在Linux上面开发,所以我装了一个virtualbox虚拟机,在该虚拟机上面安装了一个centOS系统.linux系统是装好了,但是网络配置却另人头疼.我主要是想让宿主机 ...
- 【MYSQL】创建虚表来辅助数据库查询
在进行数据库查询时,有时需要用到对既有的数据表进行多表查询得出的临时条件的数据表,就可以暂时创建成为虚表,并赋予简单明了的字段名以及临时表名. 例题a:查询出每门课程低于平均成绩的学生姓名.课程名称. ...
- 通过Sql语句导数据
在通过SQL Server向导中的SQL语句导数据时,默认情况下源表中的nvarchar字段类型会变成202,解决此问题的方法是,要重新选择一下对应的数据接收表.
- hdu----(4521)小明系列问题——小明序列
小明系列问题——小明序列 Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 65535/32768 K (Java/Others)Tota ...
- Go语言并发与并行学习笔记(二)
转:http://blog.csdn.net/kjfcpua/article/details/18265461 Go语言的并发和并行 不知道你有没有注意到一个现象,还是这段代码,如果我跑在两个goro ...
- 常用的js函数
function $(){ return document.getElementById(arguments[0])}; /** * 得到上一个元素 * @param {Object} elem */ ...
- 在单元测试中指定log4j的配置文件
在开发过程中,我们会使用到log4j来输出日志,我们希望在单元测试的时候,只看到部分日志信息,或者定义日志输出的级别. 这个时候手工指定log4j的配置文件: 具体做法如下: 定义类如下: i ...