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原理的更多相关文章

  1. iOS开发UIScrollView的底层实现

    起始 做开发也有一段时间了,经历了第一次完成项目的激动,也经历了天天调用系统的API的枯燥,于是就有了探索底层实现的想法. 关于scrollView的思考 在iOS开发中我们会大量用到scrollVi ...

  2. iOS开发·runtime原理与实践: 消息转发篇(Message Forwarding) (消息机制,方法未实现+API不兼容奔溃,模拟多继承)...

    本文Demo传送门: MessageForwardingDemo 摘要:编程,只了解原理不行,必须实战才能知道应用场景.本系列尝试阐述runtime相关理论的同时介绍一些实战场景,而本文则是本系列的消 ...

  3. IOS开发-UIScrollView陷阱之----删除所有子view, 滚动条(indicator) 消失

    使用UIScrollView经常会执行清空视图的操作,我们普遍的做法是: for (UIView *subview in self.scrollView.subviews) { [subview re ...

  4. iOS开发-UIScrollView图片无限循环

    关于UIScrollView图片浏览的例子有很多,之前也写过类似方面的文章,关于UIScrollView的图片循环在新闻类的App基本上是比较常见的一种情况就是图片浏览,然后根据不同的图片显示不同的内 ...

  5. iOS开发UIScrollView常见属性和方法

    一.ScrollView常用方法和属性 @property(nonatomic)CGPoint contentOffset; 设置滚动的偏移量 @property(nonatomic)CGSize c ...

  6. iOS开发进阶

    <iOS开发进阶>基本信息作者: 唐巧 出版社:电子工业出版社ISBN:9787121247453上架时间:2014-12-26出版日期:2015 年1月开本:16开页码:268版次:1- ...

  7. 【原】iOS开发进阶(唐巧)读书笔记(二)

    第三部分:iOS开发底层原理 1.Objective-C对象模型 1.1 isa指针 NSObject.h部分代码: NS_ROOT_CLASS @interface NSObject <NSO ...

  8. 《iOS开发进阶》书籍目录

    第一部分:iOS开发工具 第二部分:iOS开发实践 第10章 理解内存管理 10.1 引用计数 10.1.1 什么是引用计数,原理是什么 10.1.2 我们为什么需要引用计数 10.1.3 不要向已经 ...

  9. iOS开发UI篇—UIScrollView控件实现图片缩放功能

    iOS开发UI篇—UIScrollView控件实现图片缩放功能 一.缩放 1.简单说明: 有些时候,我们可能要对某些内容进行手势缩放,如下图所示 UIScrollView不仅能滚动显示大量内容,还能对 ...

随机推荐

  1. temp--test audio micphone

    DWORD CALLBACK waveInProc(HWAVEIN hWaveIn, UINT uMsg, DWORD dwInstance, DWORD dwParam1, DWORD dwPara ...

  2. mORMot 数据库操作

    程序中要使用数据库,首先是引用SynCommons, SynDB单元,根据不同的数据库类型,简单举几个例子: 1 使用Access数据库,引用SynCommons, SynDB,SynOleDb三个单 ...

  3. GATK使用说明-GRCh38(Genome Reference Consortium)(二)

    Reference Genome Components 1. GRCh38 is special because it has alternate contigs that represent pop ...

  4. Git常用命令(自己总是忘记,整理在这里)

    1.git init    初始化一个空的git仓库 2.git clone +SSH地址    clone新的项目到本地 3.git add     git add file 4.git commi ...

  5. MySQL~ IN , FIND_IN_SET , LIKE

    MySQL- IN , FIND_IN_SET , LIKE SELECT * FROM test where area IN (1, 2, 3); SELECT * FROM test where ...

  6. php 消息实时推送(反ajax推送)

    入口文件index.html <!DOCTYPE HTML> <html> <head> <title>反ajax推送</title> &l ...

  7. hdu 4268 Alice and Bob

    Alice and Bob Time Limit : 10000/5000ms (Java/Other)   Memory Limit : 32768/32768K (Java/Other) Tota ...

  8. HDU 1165 Eddy's research II (找规律)

    题意:给定一个表达式,然后让你求表达式的值. 析:多写几个就会发现规律. 代码如下: #pragma comment(linker, "/STACK:1024000000,102400000 ...

  9. vim 空格 制表符

    set tabstop=4        设定tab宽度为4个字符set shiftwidth=4     设定自动缩进为4个字符set expandtab        用space替代tab的输入 ...

  10. 删除xcode中的描述文件的路径

    打开Finder  commend + shift +g 进入文件夹  : ~/Library/MobileDevice/Provisioning Profiles  删除即可