Cat Share, must boutique
Material code Address: http://blog.csdn.net/u013357243/article/details/45305785
Original articles, welcome reprint. Reprint Please specify: Sanayu's Blog
Address: http://blog.csdn.net/u013357243?viewmode=contents
First look at the effect
The first step in the implementation process, the three view set, and color
#warning First Step- (void) addchildview{// Left UIView*leftview = [[UIViewAlloc] initWithFrame: Self. View. Bounds]; Leftview. BackgroundColor= [UicolorGreencolor]; [ Self. ViewAddsubview:leftview]; _leftview = Leftview;// Right UIView*rightview = [[UIViewAlloc] initWithFrame: Self. View. Bounds]; Rightview. BackgroundColor= [UicolorBluecolor]; [ Self. ViewAddsubview:rightview]; _rightview = Rightview;//MainView UIView*mainview = [[UIViewAlloc] initWithFrame: Self. View. Bounds]; MainView. BackgroundColor= [UicolorRedcolor]; [ Self. ViewAddsubview:mainview]; _mainview = MainView;}
The second step is to get the point, then get the x value of the finger slip offset, and then calculate the displacement that needs to be made according to the algorithm.
#warning Step Two- (void) touchesmoved: (Nsset *) touches withevent: (uievent *) event{//Get Uitouch objectUitouch *touch = [touches anyobject];//Get current point CgpointCurrentpoint = [Touch Locationinview: Self. View];//Get previous point CgpointPrepoint = [Touch Previouslocationinview: Self. View];//X-axis offset: How much x is offset when the finger moves a little bit CGFloatOffsetX = Currentpoint. x-Prepoint. x;//Set frame for current main view_mainview. Frame= [ SelfGETCURRENTFRAMEWITHOFFSETX:OFFSETX]; _isdraging =YES;}
Step three: Observe the frame change of _mainview
#warning Third step to observe _mainview's frame change //2. Monitoring /** * Add an observer to _mainview * * KeyPath: Listen for Frame this property * Options: Monitor New value changes */[_mainview Addobserver: Selfforkeypath:@"Frame"Options:nskeyvalueobservingoptionnew Context:Nil];}//When the frame property of _mainview is changed, it is called- (void) Observevalueforkeypath: (NSString*) KeyPath Ofobject: (ID) Object change: (nsdictionary*) Change Context: (void*) context{NSLog(@"%@", Nsstringfromcgrect (_mainview. Frame));if(_mainview. Frame. Origin. x<0) {//Move left //Show Right_rightview. Hidden=NO;//Hide left_leftview. Hidden=YES; }Else if(_mainview. Frame. Origin. x>0){//Move Right //Show left_rightview. Hidden=YES;//Hide Right_leftview. Hidden=NO; }}
Fourth Step
#warning Fourth Step: Acreage the frame and position reset of the current view according to the offset of the x-axis. #define HMMAXYWhen the finger is shifted a little, accordingXThe offset of the axis acreage out the current main view frame-(CGRect) Getcurrentframewithoffsetx: (cgfloat) offsetx{cgfloat screenw = [UIScreen mainScreen]. Bounds. Size. Width;CGFloat screenh = [UIScreen mainscreen]. Bounds. Size. Height;GetyAxis offset, each point of the finger movement,yAxis offset how much cgfloat OffsetY = OffsetX * HMMAXY/SCREENW;CGFloat scale = (SCREENH-2* OffsetY)/screenh;if (_mainview. Frame. Origin. x<0) {//swipe to the Left scale = (screenh +2* OffsetY)/screenh;}//Get previous frame cgrect frame = _mainview. Frame;Frame. Origin. x+ = OffsetX;Frame. Size. Height= Frame. Size. Height*scale;Frame. Size. Width= Frame. Size. Width*scale;Frame. Origin. Y= (screenh-frame. Size. Height) *0.5;return frame;}#define Hmrtarget#define HMLTARGET-220/* _mainview.frame.origin.x > SCREENW * 0.5 position to the right Cgrectgetmaxx (_mainview.frame) < SCREENW * 0.5 to the left-220 */< /c4>Positioning-(void) touchesended: (Nsset *) touches withevent: (uievent *) event{//Reset if (_isdraging = = NO && _mainvi ew. Frame. Origin. x!=0) {[UIView animatewithduration:0.25animations:^{_mainview. Frame= Self. View. Bounds;}];} cgfloat screenw = [UIScreen mainscreen]. Bounds. Size. Width;CGFloat target =0;if (_mainview. Frame. Origin. x> Screenw *0.5) {//navigate to right target = Hmrtarget;}else if (Cgrectgetmaxx (_mainview. Frame) < SCREENW *0.5) {//Navigate to the Left target = Hmltarget;} [UIView Animatewithduration:0.25animations:^{if (target) {//In need to locate left or right//getxAxis offset CGFloat OffsetX = target-_mainview. Frame. Origin. x;Sets the frame _mainview of the current main view. Frame= [Self Getcurrentframewithoffsetx:offsetx];}else{//Restore _mainview. Frame= Self. View. Bounds;} }];_isdraging = NO;}
PS: New iOS Exchange Learning Group: 304570962 can add cat qq:1764541256 or Znycat Let's study hard together.
Sanayu's Blog
Address: http://blog.csdn.net/u013357243?viewmode=contents
Cat learn iOS (26) UI iOS Drawer effect small Demo