日韩av黄I国产麻豆传媒I国产91av视频在线观看I日韩一区二区三区在线看I美女国产在线I麻豆视频国产在线观看I成人黄色短片

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 >

iOS开发-UIScrollView原理

發布時間:2023/12/2 34 豆豆
生活随笔 收集整理的這篇文章主要介紹了 iOS开发-UIScrollView原理 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

轉載:http://www.cnblogs.com/xiaofeixiang/p/5144256.html

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;//需要設置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實際上比我們實現的要復雜很多反彈效果,動量滾動,放大試圖以及涉及到的代理方法,本文就是簡單介紹一下原理,實際開發中如非特殊的必要,沒必要繼承UIView去實現UIScrollView。如果對UIScrollView有特殊需求,倒是可以繼承UIScrollView實現自己的功能~

轉載于:https://www.cnblogs.com/xiaofei76/p/5536966.html

總結

以上是生活随笔為你收集整理的iOS开发-UIScrollView原理的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。