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 ...
随机推荐
- [Git] 快速签出与更新所有远程分支.md
git-fetch 命令从远程仓库复制 heads 和 tags 信息到本地,保存在临时文件 .git/FETCH_HEAD 中以备 git-merge 命令使用. 你可以使用 git fetch 命 ...
- C#中,接口不能被实例化,但存在特例
看一个例子: interface IFoo { string Message { get; } } 则, IFoo obj = new IFoo("abd"); 将会报错:接口不能 ...
- VirtualBox Bridged 无线网卡
启动虚拟机后选择右键单击右下角的网络链接图标, 弹出的窗口中选择Bridged Adapter, wlan0 然后选择OK 查看virtual Box主页面中setting中网络的配置是否和刚才一 ...
- 【HTML】Iframe中的onload事件
当iframe.src重新指定一个url时会重新执行iframe的onload事件 <iframe id="indexFrame" name="index" ...
- DDD:Can I DDD?
下面是<实现领域驱动>的作者给出的一段话: You can implement DDD if you have: A passion for creating excellent soft ...
- DDD:两篇不错的文章
文章列表 Coding for Domain-Driven Design: Tips for Data-Focused Devs. Strengthening your domain: Aggrega ...
- [python]闭包到底是什么鬼?
这些东西是我在慕课网,廖雪峰大神的python课中学到的.自己写成笔记的形式分享给大家. 先来看一段代码: def f1(x): return x*x def new_fn(f): def fn(j) ...
- Javascript面向对象之继承
与类的创建篇一样,这里先贴出最终代码,再做详细分析: // 创建一个父类 function SuperType(){ this.company = 'alibaba'; } function SubT ...
- 如何彻底的卸载和删除Windows service
最近遇到很头疼的问题,安装到服务器的Windows Service卸载的时候出错了,结果在服务列表中就一直驻留,并且系统进程一直在运行,怎么都杀不掉. 最后终于找到办法了: 1.常规做法,批处理命令卸 ...
- 流行的ios开源项目
本文介绍一些流行的iOS的开源项目库 1.AFNetworking 更新频率高的轻量级的第三方网络库,基于NSURL和NSOperation,支持iOS和OSX.https://github.com/ ...