(material source) Cat learn iOS (26) UI iOS Drawer effect small Demo

Source: Internet
Author: User

Cat Share, must boutique

Material code Address: http://download.csdn.net/detail/u013357243/8635679
Original articles, welcome reprint. Reprint Please specify: Sanayu's Blog
Address: http://blog.csdn.net/u013357243?viewmode=contents

First look at the effect

SOURCE NYDrawViewController.h
////NYDrawViewController.h//06-Drawer Effect////Created by Apple on 14-9-1.//Copyright (c) 2014 itcast. All rights reserved.//#import <UIKit/UIKit.h>  @interface nydrawviewcontroller : uiviewcontroller @property(nonatomic,Weak,ReadOnly)UIView*mainview;@property(nonatomic,Weak,ReadOnly)UIView*leftview;@property(nonatomic,Weak,ReadOnly)UIView*rightview;@end

Nydrawviewcontroller.m

////NYDRAWVIEWCONTROLLER.M//06-Drawer Effect////Created by Apple on 14-9-1.//Copyright (c) 2014 itcast. All rights reserved.//#import "NYDrawViewController.h"  @interface nydrawviewcontroller ()@property(nonatomic,Assign)BOOLisdraging;@end @implementation nydrawviewcontroller - (void) viewdidload{//Uiviewcontroller[SuperViewdidload];additional setup after loading the view.    //1. Adding child controls[ SelfAddchildview];#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; }}#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;}#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;}#warning the fourth step#define NYMAXY//When the finger is offset a little, acreage the frame from the current view according to the offset of the x-axis- (CGRect) Getcurrentframewithoffsetx: (CGFloat) offsetx{CGFloatScreenw = [UIScreen mainscreen]. Bounds. Size. Width;CGFloatScreenh = [UIScreen mainscreen]. Bounds. Size. Height;//Gets the y-axis offset, and the y-axis offsets how much each finger moves    CGFloatOffsetY = OffsetX * NYMAXY/SCREENW;CGFloatScale = (SCREENH-2* OffsetY)/screenh;if(_mainview. Frame. Origin. x<0) {//Swipe leftScale = (screenh +2* OffsetY)/screenh; }//Get the previous frame    CGRectframe = _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;returnframe;}#define Nyrtarget#define NYLTARGET-220/* _mainview.frame.origin.x > SCREENW * 0.5 position to the right Cgrectgetmaxx (_mainview.frame) < SCREENW * 0.5 to the left-220 */< /c0>//Positioning- (void) touchesended: (Nsset *) touches withevent: (uievent *) event{//Reset    if(_isdraging = =NO&& _mainview. Frame. Origin. x!=0) {        [UIViewAnimatewithduration:0.25animations:^{_mainview. Frame= Self. View. Bounds;    }]; }CGFloatScreenw = [UIScreen mainscreen]. Bounds. Size. Width;CGFloattarget =0;if(_mainview. Frame. Origin. x> Screenw *0.5) {//Locate to the righttarget = Nyrtarget; }Else if(Cgrectgetmaxx (_mainview. Frame) < SCREENW *0.5) {//Locate to lefttarget = Nyltarget; }    [UIViewAnimatewithduration:0.25animations:^{if(target) {//need to locate the left or right            //Get x-axis offset            CGFloatOffsetX = target-_mainview. Frame. Origin. x;//Set frame for current main view_mainview. Frame= [ SelfGETCURRENTFRAMEWITHOFFSETX:OFFSETX]; }Else{//Restore_mainview. Frame= Self. View. Bounds;    }    }]; _isdraging =NO;}@end

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

(material source) Cat learn iOS (26) UI iOS Drawer effect small Demo

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.