博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
UIScrollView实现图片循环滚动
阅读量:7115 次
发布时间:2019-06-28

本文共 3574 字,大约阅读时间需要 11 分钟。

hot3.png

首先,准备工作就不说了,新建一个工程,创建一个 UIViewController 子类,并在应用中加入三个图片(1,2,3.jpg)

220620_QGfN_1394806.png

设置创建的 UIViewController 子类 为应用根视图控制对象

    AXRollController *rollController = [[AXRollController alloc] init];    [[self window] setRootViewController:rollController];

在 UIViewController 子类中添加 scrollView,imageViewOne,imageViewTwo,imageViewThree 实例变量。并重写 viewDidLoad 方法,初始化实例变量,将 ImageView 的视图都加入到 scrollView 的子视图

    /* 获取应用屏幕Frame */    CGRect screenRect = [[UIScreen mainScreen] applicationFrame];    /* 创建 UIScrollView, 并加入视图控制器视图 */    scrollView = [[UIScrollView alloc] initWithFrame:screenRect];    [scrollView setPagingEnabled:YES];    [[self view] addSubview:scrollView];    /* 获得 scroll 取景, 设置三倍宽度 */    CGRect bigRect = screenRect;    bigRect.size.width *= 3.0;    /* 设置取景宽度 */    [scrollView setContentSize:bigRect.size];        /* 创建第一个 UIImageView, 并加入到 scrollView */    imageViewOne = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"1.jpg"]];    [imageViewOne setContentMode:UIViewContentModeScaleAspectFit];    [scrollView addSubview:imageViewOne];        /* 创建第二个 UIImageView, 并加入到 scrollView */    imageViewTwo = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"2.jpg"]];    [imageViewTwo setContentMode:UIViewContentModeScaleAspectFit];    [scrollView addSubview:imageViewTwo];        /* 创建第三个 UIImageView, 并加入到 scrollView */    imageViewThree = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"3.jpg"]];    [imageViewThree setContentMode:UIViewContentModeScaleAspectFit];    [scrollView addSubview:imageViewThree];        /* 初始化三个 UIImageView 的 Frame */    [self setAllViewFrame];        /* 设置 scrollView 初始偏移量, 显示第二个 UIImageView */    CGPoint p = CGPointZero;    p.x = scrollView.frame.size.width;    [scrollView setContentOffset:p animated:NO];    /* 设置代理对象 */    [scrollView setDelegate:self];

setAllViewFrame,设置三个 UIImageView 的 Frame

    [imageViewOne setFrame:CGRectMake(scrollView.frame.origin.x,  scrollView.frame.origin.y, scrollView.frame.size.width, scrollView.frame.size.height)];    [imageViewTwo setFrame:CGRectMake(scrollView.frame.size.width, scrollView.frame.origin.y, scrollView.frame.size.width, scrollView.frame.size.height)];    [imageViewThree setFrame:CGRectMake(scrollView.frame.size.width * 2, scrollView.frame.origin.y, scrollView.frame.size.width, scrollView.frame.size.height)];

实现 UIScrollerView 的代理方法,scrollViewDidEndDecelerating:。当前 page(0,1[当前],2) 页为 1,则代表未动,返回;当前 page 页为 0,则代表右移,0->1/2->0/1->2;当前 page 页为 1,则代表左移,3->1/0->3/1->0。

    NSLog(@"%@", NSStringFromSelector(_cmd));    CGFloat pageWidth = scrView.frame.size.width;    NSLog(@"pageWidth = %f", pageWidth);    NSLog(@"offset = %f", scrView.contentOffset.x);    int page = floor((scrView.contentOffset.x - pageWidth / 2) / pageWidth) + 1;    NSLog(@"page = %d", page);    if (page == 1) {        /* 当前页,未动,返回 */        return;    } else if (page == 0) {        /* 前一页,右移 */        [self imageMoveToRight];    } else {        /* 前一页,左移 */        [self imageMoveToLeft];    }    CGPoint p = CGPointZero;    p.x = pageWidth;    [scrView setContentOffset:p animated:NO];

右移的方法,imageMoveRight

    UIImageView *tempView = imageViewTwo;    imageViewTwo = imageViewOne;    imageViewOne = imageViewThree;    imageViewThree = tempView;    /* 重设置所有 UIImageView 视图 Frame */    [self setAllViewFrame];

左移的方法,imageMoveLeft

    UIImageView *tempView = imageViewTwo;    imageViewTwo = imageViewThree;    imageViewThree = imageViewOne;    imageViewOne = tempView;    /* 重设置所有 UIImageView 视图 Frame */    [self setAllViewFrame];

基于 Xcode 5.0.2 编写

转载于:https://my.oschina.net/u/1394806/blog/192738

你可能感兴趣的文章
gns3模式与使用csr1000v
查看>>
C++string与VC++CString互转
查看>>
PHP中的java方式重载
查看>>
osx分区合并命令行操作
查看>>
迈出第一步
查看>>
xargs paste
查看>>
hadoop在windows10 64位系统下的安装
查看>>
Hibernate空指针异常-(SettingsFactory.java:169)
查看>>
SQuirreL 连接phoenix 安装配置
查看>>
Windows下安装Redis
查看>>
hadoop伪分布式搭建,运行 wordcount
查看>>
数据分析常用到的文件排序及对比命令
查看>>
SQL Server 2016下SSMS通过FULL备份数据还原指定表信息
查看>>
所有的程序员都是自学成才
查看>>
我的友情链接
查看>>
GOROOT与GOPATH
查看>>
cocoaPods 使用
查看>>
MYSQL5.7.10 安装文档
查看>>
Oracle数据库排序后分页慢的问题
查看>>
String.Format方法及参数说明
查看>>