//
// 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. PSR-0的规范。

  2. 解决淘宝sui插件后退bug

    淘宝的sui插件在微信里,第一次访问没有问题,跳转走以后,再后退回来, 插件就无法正常加载. 一下是官方给出的说法 切换到的新页面中的 js 不执行 由于浏览器安全性考虑的限制以及可能的 js 重复执 ...

  3. 使用==比较String类型

    String类型的比较 public class StringDemo { public static void main(String[] args) { String s1 = "abc ...

  4. Sublime3基础使用技巧

    1.安装SideBarEnhancements插件 ctrl+shift+p —> Install Package —> 找到SideBarEnhancements 2.安装CSS调色器: ...

  5. drupal 2016-11-3

    我随意定义了一个hook menu发现里面的内容很快就加入到了navigation menu里面.

  6. xcode6 framework missing submodule xxx 警告

    xcode6 framework missing submodule xxx 警告 从xcode6开始,iOS可以直接创建生成framework了 如: 创建 framework 项目,TFKit.f ...

  7. MariaDB 加密特性及使用方法

    版权声明:本文由吴洪辉原创文章,转载请注明出处: 文章原文链接:https://www.qcloud.com/community/article/193 来源:腾云阁 https://www.qclo ...

  8. 【转】 C++ map的基本操作和使用

    1.map简介 map是一类关联式容器.它的特点是增加和删除节点对迭代器的影响很小,除了那个操作节点,对其他的节点都没有什么影响.对于迭代器来说,可以修改实值,而不能修改key. 2.map的功能 自 ...

  9. spring来了-05-JDBC

    概述 Spring对C3P0连接池的支持很完善 Spring对jdbc提供了JdbcTemplate,来简化jdbc操作, JdbcTemplate模板工具类,类似于DbUtils组件 JDBC: p ...

  10. Qt之QRadioButton

    简述 QRadioButton部件提供了一个带有文本标签的单选框(单选按钮). QRadioButton是一个可以切换选中(checked)或未选中(unchecked)状态的选项按钮.单选框通常呈现 ...