How IOS achieves simple drawer effects with 100 lines of code _ios

Source: Internet
Author: User
Tags uikit

Objective

The simple implementation of iOS drawer effect is now used in many applications, the Internet also has a lot of examples, this article is mainly through a number of simple code to achieve, there is a need to learn together.

Here is the effect chart

The sample code is as follows

#import <UIKit/UIKit.h>

@interface mainviewcontroller:uiviewcontroller
+ (instancetype) Mainviewcontrollerwithleftviewcontroller: (Uiviewcontroller *) Leftviewcontroller Withmainpageviewcontroller: ( Uiviewcontroller *) Mainpageviewcontroller;
@end
#import "MainViewController.h" #define Kwidth self.view.frame.size.width #define Kheight self.view.frame.size.height @
Interface Mainviewcontroller () @property (Nonatomic,strong) Uiviewcontroller *LEFTVC;
@property (Nonatomic,strong) Uiviewcontroller *centervc;
@property (nonatomic,assign) BOOL Isslider;
@property (Nonatomic,strong) UIView *corverview; @end @implementation Mainviewcontroller + (instancetype) Mainviewcontrollerwithleftviewcontroller: (Uiviewcontroller *) Leftviewcontroller Withmainpageviewcontroller: (Uiviewcontroller *) mainpageviewcontroller{MainViewController *
  MAINVC = [[Mainviewcontroller alloc] init];
  MAINVC.LEFTVC = Leftviewcontroller;
  MAINVC.CENTERVC = Mainpageviewcontroller;
return MAINVC;
  }-(void) viewdidload{[Super Viewdidload];
  Self.isslider = NO;
  Self.view.backgroundColor = [Uicolor Whitecolor];
  [Self AddChildViewController:self.leftVC];
  [Self.view AddSubview:self.leftVC.view];
  [Self AddChildViewController:self.centerVC]; [Self.view AddSubview:self.centerVC.view];
Add gestures to the left and main view [self addgestureforview];
    ///Add a mask to the main view-(void) addcorverview{if (Self.corverview) {[Self.corverview Removefromsuperview];
  Self.corverview = nil;
  } Self.corverview = [[UIView alloc] initWithFrame:self.centerVC.view.bounds];
  _corverview.backgroundcolor = [[Uicolor blackcolor] colorwithalphacomponent:0.0];
[Self.centerVC.view AddSubview:self.corverView];
    //Remove main view cover-(void) removecoverview{if (Self.corverview) {[Self.corverview Removefromsuperview];
  Self.corverview = nil; ///To add gestures to the left and main view-(void) addgestureforview{uiswipegesturerecognizer *rightgesture = [[Uiswipegesturerecognizer alloc
  ] initwithtarget:self action: @selector (swiperightaction:)];
  Rightgesture.direction = Uiswipegesturerecognizerdirectionright;
  [Self.centerVC.view Addgesturerecognizer:rightgesture]; Uiswipegesturerecognizer *leftgesture = [[Uiswipegesturerecognizer alloc] initwithtarget:self action: @selector (
 Swipeleftaction:)]; Leftgesture.direction = Uiswipegesturerecognizerdirectionleft;
  [Self.centerVC.view Addgesturerecognizer:leftgesture]; Uiswipegesturerecognizer *leftvcleftswipegesture = [[Uiswipegesturerecognizer alloc] initwithtarget:self action:@
  Selector (leftvcleftswipeaction:)];
  Leftvcleftswipegesture.direction = Uiswipegesturerecognizerdirectionleft;
[Self.leftVC.view Addgesturerecognizer:leftvcleftswipegesture];
  }-(void) Swiperightaction: (ID) sender{[self MoveView:self.centerVC.view scale:0.8 panvalue:kwidth];
  Self.isslider = YES;
[Self addcorverview];
  }-(void) Swipeleftaction: (ID) sender{[self MoveView:self.centerVC.view scale:1 PANVALUE:KWIDTH/2];
  Self.isslider = NO;
[Self removecoverview];
  }-(void) Leftvcleftswipeaction: (ID) sender{[self MoveView:self.centerVC.view scale:1 PANVALUE:KWIDTH/2];
  Self.isslider = NO;
[Self removecoverview]; //pan and Zoom one view-(void) Moveview: (UIView *) View scale: (cgfloat) scale panvalue: (cgfloat) value{[UIView Beginanimations:ni L ContExt:nil];
  View.transform = Cgaffinetransformscale (Cgaffinetransformidentity,scale,scale);
  View.center = Cgpointmake (value, VIEW.CENTER.Y);
[UIView commitanimations]; } @end

Summarize

The above is the entire content of this article, I hope the content of this article for everyone's study or work can help, if there is doubt you can message exchange.

Related Article

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.