UIScrollView在開發中是不可避免,關於UIScrollView都有本身必定的理解。滾動視圖有兩個須要理解的屬性,frame和bounds,frame是定義了視圖在窗口的大小和位置,bounds表示視圖在其自身座標系中的位置和大小,frame影響視圖在窗口位置,bounds會影響子視圖的位置。atom
先來看一張圖片:spa
咱們用一個父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:blog
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的,咱們也能夠設置,並控制滑動的範圍:get
-(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實現本身的功能~博客