#import ViewController.h
#define ImageViewCount 5
 
@interface ViewController ()<uiscrollviewdelegate>
 
@property (weak, nonatomic) IBOutlet UIScrollView *imageScrollView;
@property (weak, nonatomic) IBOutlet UIPageControl *imageViewPageControl;
@property (strong, nonatomic) NSTimer *timer;
@end
 
@implementation ViewController
 
- (void)viewDidLoad {
    [super viewDidLoad];
   
    [self addImageView2ScrollView];
    self.imageScrollView.contentSize = CGSizeMake(self.imageScrollView.frame.size.width * ImageViewCount, 0);
 
    self.imageScrollView.delegate = self;
    self.imageScrollView.pagingEnabled = YES;//UIScrollView支持拖动分页
    self.imageViewPageControl.numberOfPages  = ImageViewCount;
     
    [self addScrollTimer];
}
 
- (void)rotatePic{
    int currentPageIndex = self.imageViewPageControl.currentPage;
    if(++currentPageIndex == 5){
        currentPageIndex = 0;
    }
    CGFloat offsetX = currentPageIndex * self.imageScrollView.frame.size.width;
    [UIView animateWithDuration:1 animations:^{
        self.imageScrollView.contentOffset = CGPointMake(offsetX, 0);
    }];
}
 
/**添加图片到imageScrollView*/
- (void)addImageView2ScrollView{
    CGFloat imageWidth = self.imageScrollView.frame.size.width;
    CGFloat imageHeight = self.imageScrollView.frame.size.height;
    for(int i = 0;i <= ImageViewCount;i++){
        UIImageView *imageInScroll = [[UIImageView alloc] init];
        imageInScroll.frame = CGRectMake(i * imageWidth, 0, imageWidth, imageHeight);
        imageInScroll.image = [UIImage imageNamed:[NSString stringWithFormat:@img_%02d,i + 1]];
        [self.imageScrollView addSubview:imageInScroll];
    }
}
 
// 正滚动时执行
- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
    CGFloat offX = self.imageScrollView.contentOffset.x;//(0,0)距离content内部左上顶点的x轴长度
    NSLog(@~~~~~~~%f ^^^^^^%f, offX, self.imageScrollView.frame.size.width);
    int currentPageIndex = (offX + .5f * self.imageScrollView.frame.size.width) / self.imageScrollView.frame.size.width;
    self.imageViewPageControl.currentPage = currentPageIndex;
}
 
- (void)addScrollTimer{
    self.timer = [NSTimer timerWithTimeInterval:2 target:self selector:@selector(rotatePic) userInfo:nil repeats:YES];
    [[NSRunLoop mainRunLoop] addTimer:self.timer forMode:NSRunLoopCommonModes];
}
 
- (void)removeScrollTimer{
    [self.timer invalidate];//释放定时器
    self.timer = nil;
}
 
// 开始准备滚动时执行 移除定时滚动操作
- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView{
    NSLog(@~~~scrollViewWillBeginDragging);
    [self removeScrollTimer];
}www.2cto.com
 
// 结束滚动后执行 添加定时滚动操作
- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate{
    NSLog(@~~~scrollViewDidEndDragging);
    [self addScrollTimer];
}
@end
</uiscrollviewdelegate>

对UIScrollView的运用,以上代码中有详细注释,需注意2点:

1.注意设置contentSize属性。其中contentSize表示scroll内容尺寸大小

2.注意设置代理UIScrollViewDelegate,才可调用其中的方法

对于定时器NSTimer的运用需注意

1.在线程的loop中添加定时器

2.注意使用完成回收NSTimer

结伴旅游,一个免费的交友网站:www.jieberu.com

推推族,免费得门票,游景区:www.tuituizu.com

iOS开发之实现图片自动切换(类似android画廊效果)的更多相关文章

  1. iOS开发——高级篇——图片轮播及其无限循环效果

    平时APP中的广告位.或者滚动的新闻图片等用到的就是图片轮播这种效果,实现方式主要有两种,一种是ScrollView+ImageView,另一种则是通过CollectionView,今天总结的是Scr ...

  2. iOS开发:使用Tab Bar切换视图

    iOS开发:使用Tab Bar切换视图 上一篇文章提到了多视图程序中各个视图之间的切换,用的Tool Bar,说白了还是根据触发事件使用代码改变Root View Controller中的Conten ...

  3. 原生js+css3实现图片自动切换,图片轮播

    运用CSS3transition及opacity属性 制作图片轮播动画 自己这两天根据用js来控制触发CSS3中transition属性,从而写出来的以CSS3动画为基础,js控制过程的图片轮播 运用 ...

  4. js实现图片自动切换效果。

    js实现图片自动切换效果,简单实用,原谅我只是一只小菜鸟还在学大神天天写博文装逼. <script language="javascript"> setInterval ...

  5. IOS开发中UIBarButtonItem上按钮切换或隐藏实现案例

    IOS开发中UIBarButtonItem上按钮切换或隐藏案例实现案例是本文要介绍的内容,这个代码例子的背景是:导航条右侧有个 edit button,左侧是 back button 和 add bu ...

  6. jquery 图片自动切换

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  7. tab切换效果 网站中的图片自动切换

    网站中的图片自动切换 今天上一套tab切换效果的代码 动图就自己实现吧! 下面贴HTML代码,大体分两部分,图片div和按钮div,代码很容易看懂~ <!DOCTYPE html> < ...

  8. iOS开发-iPad侧边栏Tab选项卡切换

    Android中习惯了叫侧边栏,iOS中如果不习惯侧边栏称呼的话可以叫dock,侧边栏的切换,类似于Android中的底部导航栏的切换,iPad尺寸大了一些,导航的栏目放在侧边会显示的更好耐看一些.选 ...

  9. iOS开发之虾米音乐频道选择切换效果分析与实现

    今天博客的内容比较简单,就是看一下虾米音乐首页中频道选择的一个动画效果的实现.之前用mask写过另外一种Tab切换的一种效果,网易云音乐里边的一种Tab切换效果,详情请移步于"视错觉:从一个 ...

随机推荐

  1. merge into使用方法

    此外,You cannot update a column that is referenced in the ON condition clause.,update的字段不可以是on里面的条件字段, ...

  2. JAVA问题String literal is not properly closed by a double-quote

    String literal is not properly closed by a double-quote 这个错误:string字串没有以双引号结束String DBURL = "jd ...

  3. HDU3085NightmareII题解--双向BFS

    题目链接 http://acm.hdu.edu.cn/showproblem.php?pid=3085 分析 大意就是一个男孩和一个女孩在网格里,同时还有两个鬼,男孩每轮走三步,女孩每轮走一步,与鬼曼 ...

  4. 完美解决Uncaught SyntaxError: Unexpected end of input

    Unexpected end of input  的英文意思是“意外的终止输入” 他通常表示我们浏览器在读取我们的js代码时,碰到了不可预知的错误,导致浏览器 无语进行下面的读取 通常造成这种错误的原 ...

  5. jQuery EasyUI中DataGird动态生成列的方法

    EasyUI中使用DataGird显示数据列表中,有时需要根据需要显示不同的列,例如,在权限管理中,不同的用户登录后只能查看自己权限范围内的列表字段,这就需要DataGird动态组合列,下面介绍Eas ...

  6. [yii\queue\Queue] [10] unknown job (attempt: 1, PID: 31167) is finished with error: yii\base\ErrorException: unserialize(): Error at offset 1922 of 65535 bytes

    网上的解决方案: 1. 报错场景:序列化字段中有中文,反序列化时有可能会出现报错. 错误原因:写入和取出数据库的时候,编码不同,中文符号长度不同,序列化中的长度就无法匹配. 解决办法:适合 php 5 ...

  7. Web开发的分层结构与MVC模式

    1.分层结构 所谓分层结构.把不同的功能代码封装成类,把相同功能的类封装在一个个的包中,也叫层.功能归类如下: 实体类: 封装数据,是数据的载体,在层与层之间进行传递,数据也就传递了.比如说要传递学生 ...

  8. fastadmin Excel导出时数字被科学计数

    /public/assets/libs/bootstrap-table/dist/extensions/export/bootstrap-table-export.min.js //exportOpt ...

  9. Linux系统组成和获取命令帮助3

    命令的语法通用格式:        # COMMAND OPTIONS ARGUMENTS        COMMAND:            发起一个命令:请求内核将某个二进制程序运行为一个进程  ...

  10. 移动端的文本框获取焦点时导致fixed或absolute定位的按钮被手机键盘顶上去的问题

    var win_h = $(window).height();//关键代码 window.addEventListener('resize', function () { if($(window).h ...