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 ...
随机推荐
- 【问题与思考】1+"1"=?
概述 在数学中1+1=2,在程序中1+1=2,而1+"1"=? 围绕着1+"1"的问题,我们来思考下这个问题. 目录: 一.在.Net代码中 二.在JavaSc ...
- Java 时间日期系列目录
下面是Java的时间和日期相关文章目录: 01. Java Calendar,Date,DateFormat,TimeZone,Locale等时间相关内容的认知和使用(1) Calendar 02. ...
- [OpenCV] Image Processing - Image Elementary Knowledge
"没有坚实的理论基础,实践只会浅尝于表面." 这是两本打基础的书,没系统学过的话,怎么好意思说自己会CV. 该领域,兴军亮 这个名字屡次出现,看来是计算机视觉领域国内的年轻才俊,向 ...
- 利用 ELK系统分析Nginx日志并对数据进行可视化展示
一.写在前面 结合之前写的一篇文章:Centos7 之安装Logstash ELK stack 日志管理系统,上篇文章主要讲了监控软件的作用以及部署方法.而这篇文章介绍的是单独监控nginx 日志分析 ...
- C#语法糖之 ReflectionSugar 通用反射类
用法很简单: ReflectionSugar rs = new ReflectionSugar(100);//缓存100秒 ,可以不填默认不缓存 rs.有嘛点嘛 性能测试: 性能测试类源码: ht ...
- vertical-align两种应用场合
vertical-align两种应用场合 (1)用在td/th中或display:table-cell元素中:让当前元素中的文本内容在竖直方向上居中 css部分: .content{ ...
- .net core 1.0 Web MVC 自定义认证过程
通过官方的介绍可知,若要本地开始部署搭建一个基于.net core 1.0的Web应用,需要下载dotnet SDK,或在Visual Studio IDE之上安装相关插件以布置开发环境.为了使开发环 ...
- C#设计模式——原型模式(Prototype Pattern)
一.概述 在软件开发中,经常会碰上某些对象,其创建的过程比较复杂,而且随着需求的变化,其创建过程也会发生剧烈的变化,但他们的接口却能比较稳定.对这类对象的创建,我们应该遵循依赖倒置原则,即抽象不应该依 ...
- ACCESS的参数化查询
看论坛上还许多人问及ACCESS被注入的安全问题许多人解决的方法仍然是用Replace替换特殊字符,然而这样做也并没有起到太大做用今天我就把我用ACCESS参数化查询的一些方法和经验和大家分享希望对大 ...
- BI之SSAS完整实战教程4 -- 部署至SSAS进行简单分析
上一篇已经创建了多维数据集的结构. 接下来我们将多维数据集的架构定义发送到Analysis Services实例,部署到AS上去. 文章提纲 部署和浏览多维数据集 SSMS使用简介 总结 一.部署和浏 ...