Reprint: http://www.cnblogs.com/xiaofeixiang/p/5144256.html
Uiscrollview in the development is inevitable, about Uiscrollview have their own certain understanding. Scrolling views have two properties to understand, frame and bounds,frame define the size and position of the view in the window, bounds represents the position and size of the view in its own coordinate system, frame affects the view at the window position, and bounds affects the location of the child view.
First look at a picture:
We fill the entire window with a parent view and then add a child 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:@ "blog Park-flyelephant"]; [desc Setfont:[uifont systemfontofsize:14]; [Desc Settextalignment:nstextalignmentcenter]; [Self.container Addsubview:desc]; [Self.view AddSUbview:self.container];
Reset bounds:
CGRect bounds=self.container.bounds; Bounds.origin=cgpointmake (0, +); Self.container.bounds=bounds;
The effect is as follows:
By setting bounds to let the view move up, I can simply define the Uiscrollview by gesture:
@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
The effect is as follows:
uiscrollview can be set contesize, we can also set, and control the range of sliding :
-(void) Handlepangesture: (Uipangesturerecognizer *) pangesturerecongnizer{ Cgpoint translation = [Pangesturerecongnizer translationinview:self]; CGRect bounds = self.bounds; Need to set 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 is actually a lot more complicated than what we've achieved. Rebound effect, momentum scrolling, amplification attempt and the agent method involved, this article is a brief introduction to the principle, the actual development of non-special necessary, no need to inherit uiview to achieve uiscrollview. If you have special needs for Uiscrollview, you can inherit uiscrollview to realize your function ~
iOS Development-uiscrollview Principles