iOS开发-UIScrollView原理
轉(zhuǎn)載:http://www.cnblogs.com/xiaofeixiang/p/5144256.html
UIScrollView 在開發(fā)中是不可避免,關(guān)于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];重新設(shè)置Bounds:
CGRect bounds=self.container.bounds;bounds.origin=CGPointMake(0, 200);self.container.bounds=bounds;效果如下:
通過設(shè)置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是可以設(shè)置ConteSize的,我們也可以設(shè)置,并控制滑動的范圍:
-(void)handlePanGesture:(UIPanGestureRecognizer *)panGestureRecongnizer{CGPoint translation = [panGestureRecongnizer translationInView:self];CGRect bounds = self.bounds;//需要設(shè)置contentsizeCGFloat 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實際上比我們實現(xiàn)的要復(fù)雜很多反彈效果,動量滾動,放大試圖以及涉及到的代理方法,本文就是簡單介紹一下原理,實際開發(fā)中如非特殊的必要,沒必要繼承UIView去實現(xiàn)UIScrollView。如果對UIScrollView有特殊需求,倒是可以繼承UIScrollView實現(xiàn)自己的功能~
轉(zhuǎn)載于:https://www.cnblogs.com/xiaofei76/p/5536966.html
總結(jié)
以上是生活随笔為你收集整理的iOS开发-UIScrollView原理的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 2. python 参数个数可变的函数
- 下一篇: .propertie文件注释