iOS開發-UIScrollView原理

來源:互聯網
上載者:User

標籤:

轉載: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;        //需要設定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原理

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.