iOS开发-UIScrollView原理
UIScrollView在开发中是不可避免,关于UIScrollView都有自己一定的理解。滚动视图有两个需要理解的属性,frame和bounds,frame是定义了视图在窗口的大小和位置,bounds表示视图在其自身坐标系中的位置和大小,frame影响视图在窗口位置,bounds会影响子视图的位置。
先来看一张图片:

我们用一个父View将整个窗口铺满,然后添加子视图:
UIView *redView = [[UIView alloc] initWithFrame:CGRectMake(20, 20, 100, 100)];
redView.backgroundColor = [UIColor redColor]; UIView *greenView = [[UIView alloc] initWithFrame:CGRectMake(160, 150, 150, 180)];
greenView.backgroundColor = [UIColor greenColor]; UIView *blueView = [[UIView alloc] initWithFrame:CGRectMake(60, 400, 200, 150)];
blueView.backgroundColor = [UIColor blueColor]; UIView *yellowView = [[UIView alloc] initWithFrame:CGRectMake(180, 600, 180, 200)];
yellowView.backgroundColor = [UIColor yellowColor]; [self.container addSubview:redView];
[self.container addSubview:greenView];
[self.container addSubview:blueView];
[self.container addSubview:yellowView];
UILabel *desc=[[UILabel alloc]initWithFrame:CGRectMake(150, 20, 200, 20)];
[desc setText:@"博客园-FlyElephant"];
[desc setFont:[UIFont systemFontOfSize:14]];
[desc setTextAlignment:NSTextAlignmentCenter];
[self.container addSubview:desc];
[self.view addSubview:self.container];
重新设置Bounds:
CGRect bounds=self.container.bounds;
bounds.origin=CGPointMake(0, 200);
self.container.bounds=bounds;
效果如下:

通过设置Bounds可以让视图向上移动,那么我可以通过手势简单的定义UIScrollView:
@interface FEScrollView()
@property (assign,nonatomic) CGSize contentSize;
@end
@implementation FEScrollView
-(instancetype)initWithFrame:(CGRect)frame{
self=[super initWithFrame:frame];
if (self) {
UIPanGestureRecognizer *panGesture=[[UIPanGestureRecognizer alloc]initWithTarget:self action:@selector(handlePanGesture:)];
[self addGestureRecognizer:panGesture];
}
return self;
}
-(void)handlePanGesture:(UIPanGestureRecognizer *)panGestureRecongnizer{
CGPoint translation = [panGestureRecongnizer translationInView:self];
CGRect bounds = self.bounds;
CGFloat newBoundsOriginY = bounds.origin.y - translation.y;
bounds.origin.y=newBoundsOriginY;
self.bounds = bounds;
[panGestureRecongnizer setTranslation:CGPointZero inView:self];
}
@end
效果如下:

UIScrollView是可以设置ConteSize的,我们也可以设置,并控制滑动的范围:
-(void)handlePanGesture:(UIPanGestureRecognizer *)panGestureRecongnizer{
CGPoint translation = [panGestureRecongnizer translationInView:self];
CGRect bounds = self.bounds;
//需要设置contentsize
CGFloat newBoundsOriginX = bounds.origin.x - translation.x;
CGFloat minBoundsOriginX = 0.0;
CGFloat maxBoundsOriginX = self.contentSize.width - bounds.size.width;
bounds.origin.x = fmax(minBoundsOriginX, fmin(newBoundsOriginX, maxBoundsOriginX));
CGFloat newBoundsOriginY = bounds.origin.y - translation.y;
CGFloat minBoundsOriginY = 0.0;
CGFloat maxBoundsOriginY = self.contentSize.height - bounds.size.height;
bounds.origin.y = fmax(minBoundsOriginY, fmin(newBoundsOriginY, maxBoundsOriginY));
self.bounds = bounds;
[panGestureRecongnizer setTranslation:CGPointZero inView:self];
}
UIScrollView实际上比我们实现的要复杂很多反弹效果,动量滚动,放大试图以及涉及到的代理方法,本文就是简单介绍一下原理,实际开发中如非特殊的必要,没必要继承UIView去实现UIScrollView。如果对UIScrollView有特殊需求,倒是可以继承UIScrollView实现自己的功能~
iOS开发-UIScrollView原理的更多相关文章
- iOS开发UIScrollView的底层实现
起始 做开发也有一段时间了,经历了第一次完成项目的激动,也经历了天天调用系统的API的枯燥,于是就有了探索底层实现的想法. 关于scrollView的思考 在iOS开发中我们会大量用到scrollVi ...
- iOS开发·runtime原理与实践: 消息转发篇(Message Forwarding) (消息机制,方法未实现+API不兼容奔溃,模拟多继承)...
本文Demo传送门: MessageForwardingDemo 摘要:编程,只了解原理不行,必须实战才能知道应用场景.本系列尝试阐述runtime相关理论的同时介绍一些实战场景,而本文则是本系列的消 ...
- IOS开发-UIScrollView陷阱之----删除所有子view, 滚动条(indicator) 消失
使用UIScrollView经常会执行清空视图的操作,我们普遍的做法是: for (UIView *subview in self.scrollView.subviews) { [subview re ...
- iOS开发-UIScrollView图片无限循环
关于UIScrollView图片浏览的例子有很多,之前也写过类似方面的文章,关于UIScrollView的图片循环在新闻类的App基本上是比较常见的一种情况就是图片浏览,然后根据不同的图片显示不同的内 ...
- iOS开发UIScrollView常见属性和方法
一.ScrollView常用方法和属性 @property(nonatomic)CGPoint contentOffset; 设置滚动的偏移量 @property(nonatomic)CGSize c ...
- iOS开发进阶
<iOS开发进阶>基本信息作者: 唐巧 出版社:电子工业出版社ISBN:9787121247453上架时间:2014-12-26出版日期:2015 年1月开本:16开页码:268版次:1- ...
- 【原】iOS开发进阶(唐巧)读书笔记(二)
第三部分:iOS开发底层原理 1.Objective-C对象模型 1.1 isa指针 NSObject.h部分代码: NS_ROOT_CLASS @interface NSObject <NSO ...
- 《iOS开发进阶》书籍目录
第一部分:iOS开发工具 第二部分:iOS开发实践 第10章 理解内存管理 10.1 引用计数 10.1.1 什么是引用计数,原理是什么 10.1.2 我们为什么需要引用计数 10.1.3 不要向已经 ...
- iOS开发UI篇—UIScrollView控件实现图片缩放功能
iOS开发UI篇—UIScrollView控件实现图片缩放功能 一.缩放 1.简单说明: 有些时候,我们可能要对某些内容进行手势缩放,如下图所示 UIScrollView不仅能滚动显示大量内容,还能对 ...
随机推荐
- Linux档案与目彔的基本操作(查看与权限)
此文包含的命令: cd.pwd.mkdir.rmdir.rm.ls.cp.mv.cat.tac.more.less.head.tail.od.touch.umask.chattr.lsattr.fil ...
- python学习笔记-Day6(3)
代码书写原则: 1)不能重复写代码 2)写的代码要经常变更 编程模式概述 面向过程:根据业务逻辑从上到下写垒代码 函数式:将某功能代码封装到函数中,日后便无需重复编写,仅调用函数即可 面向对象:对函数 ...
- jQuery easyui treegrid无法传参到后台bugger一记
$("#lTreegrid").treegrid("options").queryParams={id:123456,name:"Hai he&quo ...
- Android之ImageView 旋转
方案一: (1)获取ImageView对应的图片,并将其转成Bitmap; (2)创建Matrix对象; (3)调用matrix.setRotate( );设置旋转度数 (4)重新创建bitm ...
- Ubuntu系统监控cpu memery 磁盘Io次数 IO速率 网卡 运行时间等信息的采集
实验室最近在做的项目要做ubuntu系统监控,要获得系统的一些信息并返回给web服务器. web服务器与ubuntu主机的通信我写的程序用的是socket,至于为什么不用java程序ssh到对应的主机 ...
- 使用servers 启动项目时 ,一直处于启动中, 最后出现无法的问题。
使用eclipse 中的servers 配置了一个server 来启动项目, 发现无法启动 排除法: 去掉项目配置,单独启动该server ,发现可以启动, 说明是项目出现问题 但是项目并没有报错, ...
- 用js识别是否360浏览器
360浏览器说实话比较恶,以前可以通过内核返回的字符串判定是否包含"360"字样的方法来判定 如以下代码 window.navigator.userAgent.indexOf('3 ...
- UWP深入学习五: 传感器与搜索、共享及链接
Responding to motion and orientation sensors: Quickstart: Responding to user movement with the accel ...
- stringgrid事件大全
3.StringGrid之Event事件 property LiveBinding //把对象之间的属性绑定起来(XE2新增功能) property OnApplyStyleLookup: TNo ...
- vim/vi 复制,删除,粘贴,查找,替换
1.删除行 光标所在单行,dd 光标所在行以下的5行,5dd 全部删除:dG 2.复制 复制光标所在单行:yy 复制光标以下4行:4yy 3.粘贴 粘贴vi里复制的内容:p 粘贴外部复制过来的内容: ...