iOS— UIScrollView和 UIPageControl之间的那些事
本代码主要实现在固定的位置滑动图片可以切换。
目录图如下:
ViewController.h
#import <UIKit/UIKit.h>
// 通过宏定义定义宽和高
#define WIDTH self.view.frame.size.width
#define HEIGHT self.view.frame.size.height @interface ViewController : UIViewController<UIScrollViewDelegate> @property(strong,nonatomic) UIScrollView *myScrollView;
@property(strong,nonatomic) UIPageControl *myPageControl;
// 储存图片的集合
@property(strong,nonatomic)NSMutableArray *imageArray; // 储存照片
@property(strong,nonatomic)UIImageView *firstimage;
@property(strong,nonatomic) UIImageView *secondimage;
@property(strong,nonatomic) UIImageView *thirimage;
// 当前页码
@property(assign,nonatomic)int currentPage; @end
ViewController.m
#import "ViewController.h" @interface ViewController () @end @implementation ViewController - (void)viewDidLoad {
[super viewDidLoad];
self.myScrollView=[[UIScrollView alloc] initWithFrame:CGRectMake(, , WIDTH, HEIGHT)];
self.myScrollView.backgroundColor=[UIColor colorWithRed:0.315 green:0.843 blue:0.892 alpha:1.000];
self.myScrollView.contentSize=CGSizeMake(*WIDTH, HEIGHT);
// 设置分页
self.myScrollView.pagingEnabled=YES;
// 隐藏滚动条
self.myScrollView.showsHorizontalScrollIndicator=NO;
// 锁定滚动方向
self.myScrollView.directionalLockEnabled=YES;
// 引用代理
self.myScrollView.delegate=self; [self.view addSubview:self.myScrollView]; self.myPageControl=[[UIPageControl alloc] init];
CGSize PageSize=CGSizeMake(, );
self.myPageControl.frame=CGRectMake((WIDTH-PageSize.width)/, HEIGHT-PageSize.height-, PageSize.width, PageSize.height);
self.myPageControl.backgroundColor=[UIColor clearColor]; // 设置当前页
self.myPageControl.currentPage=;
// 分页
self.myPageControl.numberOfPages=; [self.view addSubview:self.myPageControl]; // 初始化储存图片的集合
self.imageArray=[NSMutableArray arrayWithCapacity:];
for (int i=; i<; i++) {
UIImage *image=[UIImage imageNamed:[NSString stringWithFormat:@"%d",i]];
[self.imageArray addObject:image];
} self.firstimage =[[UIImageView alloc] init];
self.secondimage=[[UIImageView alloc] init];
self.thirimage =[[UIImageView alloc] init];
// 当前页码
self.currentPage=; [self reloadImage]; } -(void)reloadImage
{
// 第一种情况, 第一页
if (self.currentPage==) {
self.firstimage.image=[self.imageArray lastObject];
self.secondimage.image=[self.imageArray objectAtIndex:self.currentPage];
self.thirimage.image=[self.imageArray objectAtIndex:self.currentPage+]; } // 第二种情况,最后一页
else if (self.currentPage==self.imageArray.count-){
self.firstimage.image=[self.imageArray objectAtIndex:self.currentPage-];
self.secondimage.image=[self.imageArray objectAtIndex:self.currentPage];
self.thirimage.image=[self.imageArray objectAtIndex:]; } // 第三种情况 中间页
else{
self.firstimage.image=[self.imageArray objectAtIndex:self.currentPage-];
self.secondimage.image=[self.imageArray objectAtIndex:self.currentPage];
self.thirimage.image=[self.imageArray objectAtIndex:self.currentPage+];
} self.firstimage.frame=CGRectMake(, , WIDTH, HEIGHT);
self.secondimage.frame=CGRectMake(WIDTH, , WIDTH, HEIGHT);
self.thirimage.frame=CGRectMake(WIDTH*, , WIDTH, HEIGHT); [self.myScrollView addSubview:self.firstimage];
[self.myScrollView addSubview:self.secondimage];
[self.myScrollView addSubview:self.thirimage]; self.myScrollView.contentOffset=CGPointMake(WIDTH, );
} #pragma mark scrollView Delegate
//在滚动结束状态转换
-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
float x=self.myScrollView.contentOffset.x; // 向左
if (x<||x==) {
if (self.currentPage==) {
self.currentPage=(int)self.imageArray.count-;
}else{
self.currentPage--;
}
} // 向右
if (x>*WIDTH||x==*WIDTH) {
if (self.currentPage==(int)self.imageArray.count-) {
self.currentPage=;
}else{
self.currentPage++;
}
} self.myPageControl.currentPage=self.currentPage;
[self reloadImage];
}
iOS— UIScrollView和 UIPageControl之间的那些事的更多相关文章
- UIScrollview 与 Autolayout 的那点事
原文 http://www.cocoachina.com/ios/20151221/14757.html 前言 自从写了 介绍Masonry 那篇文章以后 就一直有人对UIScrollView的那个 ...
- 06 (OC)* iOS中UI类之间的继承关系
iOS中UI类之间的继承关系 此图可以更好的让你去理解iOS中一些底层的关系.你能够了解以及理解UI类之间的继承关系,你会更加明白苹果有关于底层的东西,更有助于你的项目开发由它们的底层关系,就能更加容 ...
- iOS上架ipa上传问题那些事
iOS上架ipa上传问题那些事 原文: http://www.jianshu.com/p/1e22543285c2 字数513 阅读312 评论0 喜欢1 通过xcode直接打包上传,不会提示你的ip ...
- 示例详解:UIScrollview 与 Autolayout 的那点事
前言 自从写了介绍Masonry那篇文章以后 就一直有人对UIScrollView的那个例子不是很理解 UIView *container = [UIView new]; [scrollView ad ...
- 那些在学习iOS开发前就应该知道的事(part 2)
英文原文:Things I wish I had known before starting iOS development—Part 2 http://www.cocoachina.com/ios/ ...
- [IOS UIScrollView+PageControl]信息展示横幅
ScrollViewController.h #import <UIKit/UIKit.h> @interface ScrollViewController : UIViewControl ...
- UI:UIScrollView、UIPageControl
一.UIScrollView的常⽤用属性 二.UIScrollView的常⽤用代理方法 三.UIPageControl的使⽤用 四.UIPageControl与UIScrollView的结合使⽤用 U ...
- UIScrollView和UIPageControl学习使用
# UIScrollView和UIPageControl # 概要 对于同一个页面需要展示很多图片信息.子视图等的这样的需求,我们可以采用控件UIScrollVIew,与之常常一起使用的控件是UIPa ...
- IOS UIScrollView常用代理方法
iOS UIScrollView代理方法有很多,从头文件中找出来学习一下 //只要滚动了就会触发 - (void)scrollViewDidScroll:(UIScrollView *)scrollV ...
随机推荐
- apache 500错误
一直以为开了error_log,没想没有加,于是折腾了好久. 开启error_log后,发现是xdebug的max_nesting_level值太小了. 还一个原因是.htaccess文件中的 < ...
- Unity 坐标系
Unity 使用的是左手坐标系
- ruby -- 进阶学习(四)paperclip上传中文命名图片
Paperclip -- 上传中文命名图片 使用Paperclip和ImageMagick插件来处理图片的时候,上传非中文命名的图片时,只要把配置写好就没问题 if you need to ...
- ruby -- 问题解决(一)无法连接mysql数据库
>rails g controller home index 运行该命令时无法连接mysql 先下载配置文件:mysql-connector-c-noinstall-6.0.2-win32. ...
- python读取xml文件
关于python读取xml文章很多,但大多文章都是贴一个xml文件,然后再贴个处理文件的代码.这样并不利于初学者的学习,希望这篇文章可以更通俗易懂的教如何使用python 来读取xml 文件. 什么是 ...
- Direct3D11学习:(五)演示程序框架
转载请注明出处:http://www.cnblogs.com/Ray1024 一.概述 在此系列最开始的文章Direct3D11学习:(一)开发环境配置中,我们运行了一个例子BoxDemo,看过这个例 ...
- 微软IIS对http keep-alive的“霸道”处理
大家都知道在IIS中有个HTTP keep-alive设置,见下图: 很多人可能和我们一样,以为这样设置后,IIS会就在发送响应内容时加上这个http header——Connection: keep ...
- SQL语句技巧:查询时巧用OR实现逻辑判断
首先看以下SQL逻辑语句块: ) ) SET @fieldname='chassisno' --这里可传入chassisno,plateno,owner,contacttelno其中之一或不传 SET ...
- 【推荐】oc解析HTML数据的类库(爬取网页数据)
TFhpple是一个用于解析html数据的第三方库,本人感觉功能还算可以,只不过在使用前必须配置项目. 配置 1.导入libxml2.tbd 2.设置编译路径 使用 这里使用一个例子来说明 http: ...
- Spring总结——AOP、JDBC和事务的总结
1.上一次总结了 Spring 的核心三大组件(Core,Beans,Context),今天总结的 AOP.JDBC和事务都可以看成是核心三大组件的应用. 其中 Spring 的事务管理又以 AOP ...