//
// 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 学习 广告图片轮播器的更多相关文章

  1. ios之无限图片轮播器的实现

    首先实现思路:整个collectionView中只有2个cell.中间始终显示第二个cell. 滚动:向前滚动当前cell的脚标为0,向后滚动当前的cell脚标为2.利用当前cell的脚标减去1,得到 ...

  2. UIScrollView实现图片轮播器及其无限循环效果

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

  3. IOS第六天(3:scrollView 图片轮播器)

    IOS第六天(3:scrollView 图片轮播器) #import "HMViewController.h" #define kImageCount 5 @interface H ...

  4. ios之无限 自动 图片轮播器的实现

    比较之前发布的手动无限图片轮播器进行了改进.实现了自动无限轮播的功能.比较适合团购标题分类下面的轮播器功能. 实现思路: * 开启一个定时器,把操作放入消息循环池.每隔一定时间,操作执行一次. * 注 ...

  5. iOS 简易无限滚动的图片轮播器-SDCycleScrollView

    @interface ViewController () <</span>SDCycleScrollViewDelegate> @end @implementation Vie ...

  6. ios开发图片轮播器以及定时器小问题

    一:图片轮播器效果如图:能实现自动轮播,到最后一页时,轮播回来,可以实现拖拽滚动 二:代码: #import "ViewController.h" ; @interface Vie ...

  7. UIScrollView实现图片轮播器的无限滚动

    简介 在现在的一些App中常常见到图片轮播器,一般用于展示广告.新闻等数据,在iOS内并没有现成的控件直接实现这种功能,但是通过UIScrollView的允许分页设置,可以实现滚动轮播的功能. 轮播原 ...

  8. JavaScript图片轮播器

    先贴上html的代码 <div class="ImgDiv"> <div class="Imgs" id="imgScroll&qu ...

  9. 推荐一款超级漂亮的HTML5 CSS3的图片轮播器

    最近在学习HTML5和CSS3,印象最深的是CSS3的动画功能,不仅有浏览器原生支持,执行效率高,而且免去在js中自己管理timer. 本来想写一个图片轮播器练练手,结果在网上发现一个国人写的开源的图 ...

随机推荐

  1. go切片

    本文实例讲述了GO语言数组和切片的用法.分享给大家供大家参考.具体分析如下: 一.数组 与其他大多数语言类似,Go语言的数组也是一个元素类型相同的定长的序列. (1)数组的创建. 数组有3种创建方式: ...

  2. Linux命令:traceroute(windows中为tracert)

    通过traceroute 我们可以知道信息从你的计算机到互联网另一端的主机是走的什么路径.当然每次数据包由某一同样的出发点(source)到达某一同样的目的地(destination)走的路径可能会不 ...

  3. 工作流学习——Activiti流程变量五步曲 (zhuan)

    http://blog.csdn.net/zwk626542417/article/details/46648139 ***************************************** ...

  4. android程序中界面太大太长太宽如何滚动?

    使用ScrollView即可. ScrollView只能容纳一个直接的子控件. 在Android中编写布局一般会用到scrollview嵌套LinearLayout,使页面可以自适应其高度.但是有的机 ...

  5. idea14教程

    破解:http://blog.csdn.net/guofeng526/article/details/47043457 使用教程: http://www.phperz.com/article/15/0 ...

  6. eclipse下安装插件

    最近想自己弄弄Python,手上就有eclipse,也不想在安装别的IDE占空间,就在网上找了一下eclipse支持开发python的插件,果然有. pydev官网地址:http://pydev.or ...

  7. redirect模块的秘密

    所有的redirect记录都在config/url_directs下面, 但是某个node/edit的redirect记录只包含redirect到自己的记录,且不验证url的合理性. 现在比如,a跳转 ...

  8. mysql+tomcat+spring 配置心得(从0开始搭环境)

    前几天公司竞一个标要做个POC,前端AugularJs做mobile界面,后端Spring MVC做Restful API供前端调用. 时间紧急,大家都忙,so我这个不写代码的闲人花一上午配了一套Sp ...

  9. C++从键盘输入文件结束符

            当我们使用一个istream对象作为条件时,其效果是检测流的状态.如果流是有效的,即流未遇到错误,那么检测成功.当遇到文件结束符,或遇到一个无效输入时(例如需要将输入读到一个int变量 ...

  10. DOM事件流

    DOM事件标准定义了两种事件流:Capture(捕获)和Bubbing(冒泡):捕获和冒泡是javascript针对dom事件处理的先后顺序,所谓的先后顺序是指针对父标签与其嵌套子标签,如果父标签与嵌 ...