標籤:
轉載: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原理