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 ...
随机推荐
- CSS 布局属性(display,float,clear,visibility,overflow,overflow-x,overflow-y)
display:none | inline | block | list-item | inline-block | table | inline-table | table-caption | ta ...
- jquery 按钮效果 正常、移上、按下
在网页设计过程中,经常遇见按钮的各状态效果.写了一个jquery扩展,使这个过程更方便! 使用前注意引用Jquery: JqueryExtend.js: (function ($) { // Butt ...
- [linux]重拾linux
起因 因为想重拾起linux,同时需要用docker起几个镜像,用来学习网络知识.本来想直接去阿里云上买,后来一想自己机器上,起一个linux是个不错的选择,毕竟不花钱! 还可以用来做本地测试,学习使 ...
- CSS魔法堂:盒子模型简介
本文讨论的是块级盒子(Block-level box)的盒子模型(Box Model) 一.W3C标准的盒子模型 二.IE盒子模型 三.两种模型的区别 W3C标准盒子模型: 外盒模型 元素空间宽度 ...
- HQueue:基于HBase的消息队列
HQueue:基于HBase的消息队列 凌柏 1. HQueue简介 HQueue是一淘搜索网页抓取离线系统团队基于HBase开发的一套分布式.持久化消息队列.它利用HTable存储消息数据 ...
- 用C#开发的双色球走势图(一)
首先声明,个人纯粹无聊之作,不作商业用途. 我相信每个人都拥有一个梦想那就是有朝一日能中500W,这个也一直是我的梦想,并默默每一期双色球或多或少要贡献自己一点点力量,本人并不属于那种铁杆的彩票迷,每 ...
- sql跨数据库转移
结构一样的话insert into 数据库A.dbo.TableAselect * from 数据库B.dbo.TableA 另外:nsert into DDD(字段1,字段2,字段3 .....)( ...
- GridView 用 checkbox 全选并取值
方法一,用 js 实现 <script type="text/javascript"> //<![CDATA[ function CheckAll(oCheckb ...
- WebApi传参总动员(填坑)
本以为系列文章已经Over,突然记起来前面留了个大坑还没填,真是自己给自己挖坑. 这个坑就是: (body 只能被读取一次)Only one thing can read the body MVC和W ...
- 【Bootstrap基础学习】05 Bootstrap学习总结
好吧,Copy了几天,这个总结算是把我对Bootstrap的一些理解写一下吧. Bootstrap只是一套别人写好的前端框架,直接拿来用就好. 不过对于专业的前端而言,如果不去把所有的代码都看一遍来理 ...