让图片自动滚动的话,需要使使用定时器,循环计算当前页的页码。并且在拖动图片时停止计时器,停止拖动时启动计时器。

定时器

  • 方法1: performSelector 
     [self performSelector:@selector(hideHUD) withObject:nil afterDelay:2.0];
- (void)hideHUD
{
_hudLable.hidden = YES;
}
  • 方法2: GCD
     dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.5 * NSEC_PER_SEC)),
dispatch_get_main_queue(), ^{
_hudLable.hidden = YES;
});
  • 方法3: scheduledTimerWithTimeInterval
     [NSTimer scheduledTimerWithTimeInterval:3.0
target:self selector:@selector(hideHUD) userInfo:nil repeats:NO];
  • 方法4:timerWithTimeInterval
     // 1 创建定时器,由系统控制其生命周期
[NSTimer scheduledTimerWithTimeInterval:2.0 target:self selector:@selector(nextPage) userInfo:nil repeats:YES];
// 2 创建定时器,不过是局部变量
//NSTimer *timer = [NSTimer timerWithTimeInterval:2.0 target:self selector:@selector(nextPage) userInfo:nil repeats:YES];
// 手动启动定时器
//[timer fire]; - (void)nextPage
{
NSLog(@"nextPage:%ld", self.pageControl.currentPage); }
  • 设置定时器线程的优先级
 - (void)startTimer
{
// 创建定时器
self.timer = [NSTimer scheduledTimerWithTimeInterval:2.0 target:self selector:@selector(nextPage) userInfo:nil repeats:YES];
// 设置计时器线程的优先级和其他线程一样
[[NSRunLoop mainRunLoop] addTimer:self.timer forMode:NSRunLoopCommonModes];
}

代码实现

  1、添加计时器属性

 @property (strong, nonatomic ) NSTimer *timer; //计时器

  2、在初始化完毕xib界面后,开启定时器

 // xib 初始化完毕会调用这个方法
- (void)awakeFromNib
{
// NSLog(@"awakeFromNib-----"); // 开启定时器
[self startTimer];

  3、startTimer方法实现:创建定时器

 - (void)startTimer
{
// 创建定时器
self.timer = [NSTimer scheduledTimerWithTimeInterval:2.0 target:self selector:@selector(nextPage) userInfo:nil repeats:YES];
// 设置计时器线程的优先级和其他线程一样
[[NSRunLoop mainRunLoop] addTimer:self.timer forMode:NSRunLoopCommonModes];
}

  4、实现nextPage方法,滚动到下一个界面

 - (void)nextPage
{
NSLog(@"nextPage---");
// 下一个页面,对总数取余的页码,循环滚动
NSInteger page = (self.pageControl.currentPage + ) % self.scrollView.subviews.count;
// 计算偏移量,索引值和scrollView宽度的积
CGPoint offset = CGPointMake(page * self.scrollView.frame.size.width, );
// 设置新的偏移量
[self.scrollView setContentOffset:offset animated:YES];
}

  5、实现代理方法<UIScrollViewDelegate>:拖动状态

 #pragma mark - 定时器
- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate
{
// 没有拖动图片就开始定时器
[self startTimer];
} - (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView
{
// 一旦开始拖动就停止计时器
[self stopTimer];
}

  6、停止和启动定时器方法

 - (void)startTimer
{
// 创建定时器
self.timer = [NSTimer scheduledTimerWithTimeInterval:2.0 target:self selector:@selector(nextPage) userInfo:nil repeats:YES];
// 设置计时器线程的优先级和其他线程一样
[[NSRunLoop mainRunLoop] addTimer:self.timer forMode:NSRunLoopCommonModes];
} - (void)stopTimer
{
[self.timer invalidate]; // 停止计时器
self.timer = nil; //清空指针
}

以上步骤结束,scrollView就可以自动滚动了。

虽然可以自动滚动了,但是不会循环滚动,接下来就研究一下循环滚动的方法。

IOS开发学习笔记035-UIScrollView-自动滚动的更多相关文章

  1. IOS开发学习笔记036-UIScrollView-循环自动滚动

    实现scrollView的自动循环滚动,需要实现几个方法. 其中scrollView中始终保存三张图片,其他的图片在滚动到时再进行加载. 循环的实现主要是在setUpdate 中,如果索引为0是第一个 ...

  2. iOS开发学习笔记:基础篇

    iOS开发需要一台Mac电脑.Xcode以及iOS SDK.因为苹果设备都具有自己封闭的环境,所以iOS程序的开发必须在Mac设备上完成(当然,黑苹果应该也是可以的,但就需要花很多的精力去折腾基础环境 ...

  3. iOS开发学习--纯代码 UIScrollView 无限循环的实现——代码类封装

    一个简单的利用UIScrollView 实现的无线滚动banner,下面的代码实现,因为封装问题,对两个及一下的view 支持出了一点问题(view是传参进来的,不可以生成两份),但是原理是正确的,智 ...

  4. ios开发学习笔记(1)

    objective-c基础总结 第一二章 1.application:didiFinishLauchingWithOptions:程序启动后立即执行 2.启动界面代码格式:self.window = ...

  5. IOS开发学习笔记033-UIScrollView

    1.滚动显示图片 如果图片过大,则需要滚动显示,这是需要用到类UIScrollView,可是实现控件的水平和垂直滚动. 可用三步实现:1 设置UIScrollView,2 设置UIImageView, ...

  6. ios开发学习笔记(这里一定有你想要的东西,全部免费)

    1,Search Bar 怎样去掉背景的颜色(storyboard里只能设置background颜色,可是发现clear Color无法使用). 其实在代码里还是可以设置的,那就是删除背景view [ ...

  7. IOS开发学习笔记034-UIScrollView-xib实现分页

    通过xib实现分页功能的封装 1.首先实现xib UIView 的尺寸为300*130,因为准备的图片为600*260. scrollView属性设置如下: 2.新建一个和xib同名的类 2.1 类方 ...

  8. iOS开发学习笔记

    1 常用的第三方工具 1.1 iPhone Simulator 测试程序需要模拟器iPhone Simulator 1.2 设计界面需要Interface Builder,Interface Buil ...

  9. IOS开发学习笔记026-UITableView的使用

    UITableView的简单使用过程 简单介绍 两种样式 UITableViewStylePlain UITableViewStyleGrouped 数据显示需要设置数据源,数据源是符合遵守协议 &l ...

随机推荐

  1. C/C++ sort函数的用法

    sort函数的用法(#include<algorithm>) 做ACM题的时候,排序是一种经常要用到的操作.如果每次都自己写个冒泡之类的O(n^2)排序,不但程序容易超时,而且浪费宝贵的比 ...

  2. Android 使用greenDAO 3.2.2 操作外部数据库

    项目开发中有时需要用到一些写死的数据,如公司的产品信息之类的.这就需要我们先把数据库文件保存在资源文件夹下,然后当应用创建时将数据库文件拷到应用安装目录的/databases/文件夹下,然后再对数据进 ...

  3. jquery-weui picker组件实现只选择年月

    var date = new Date() var month = date.getMonth()+1 //获取当前月份 $('#selectTime').picker({ toolbarTempla ...

  4. Flexbox与Grid属性比较

    网格容器(container)属性 网格项目(item)属性 Flex容器(container)属性 Flex项目(item)属性

  5. IOS 当一个控件被添加到父控件中会调用(didMoveToSuperview)

    /** * 当一个控件被添加到父控件中就会调用 */ - (void)didMoveToSuperview { if (self.group.opened) { self.nameView.image ...

  6. 未能加载文件或程序集“System.Web.Http, Version=5.1.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35”或它的某一个依赖项。找到的程序集清单定义与程序集引用不匹配。 (异常来自 HRESULT:0x80131040)解决办法

    1.查看引用处是否确实引用, 2.查看<runtime> <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1& ...

  7. mkfs.xfs 命令找不到的解决方法

    对硬盘进行格式化: # mkfs.xfs /dev/sdb1 系统显示: mkfs.xfs error: command not found. 可能是系统不完全安装 运行 which mkfs  查看 ...

  8. python_16_自己建立模块

    import python_5_password

  9. python-的多线程处理

    书到用时方恨少,这句话在软件杯真的深深体会到了.但是对自己对于代码的领会能力还是有自信的,在做项目的时候用到了多线程的处理,开始都不知道该怎么去百度搜索关键词

  10. SummerVocation_Learning--java的线程死锁

    public class Test_DeadLock implements Runnable { ; static Object o1 = new Object(),o2 = new Object() ...