IOS Drawer effect development case sharing _ios

Source: Internet
Author: User
Tags uikit

This example for you to share the iOS drawer effect development examples for your reference, the specific contents are as follows

Add three view to the controller displayed on the window (add only 2 view if you only need to slide one side)

Declare three view first

#import "ViewController.h"
 
@interface viewcontroller ()
@property (nonatomic, weak) UIView *mainv;
@property (nonatomic, weak) UIView *leftv;
@property (nonatomic, weak) UIView *rightv;
@end

Add view to Controller view

#pragma mark-Add child control-(void) Setupchildviews {UIView *LEFTV = [[UIView alloc]initwithframe:self.view.bounds];
   
  Leftv.backgroundcolor = [Uicolor Orangecolor];
   
  [Self.view ADDSUBVIEW:LEFTV];
   
  _LEFTV = LEFTV;
   
  UIView *rightv = [[UIView alloc]initwithframe:self.view.bounds];
   
  Rightv.backgroundcolor = [Uicolor Grouptableviewbackgroundcolor];
   
  [Self.view Addsubview:rightv];
   
  _rightv = Rightv;
   
  UIView *MAINV = [[UIView alloc]initwithframe:self.view.bounds];
   
  Mainv.backgroundcolor = [Uicolor Yellowcolor];
   
  [Self.view ADDSUBVIEW:MAINV];
_MAINV = MAINV;
   
  }-(void) viewdidload {[Super viewdidload];
   
  Add child controls [self setupchildviews];
   
  Add pan gesture Uipangesturerecognizer *pan = [Uipangesturerecognizer alloc]initwithtarget:self action: @selector (pan:)];
   
  [Self.view Addgesturerecognizer:pan]; Add-click Gestures, click anywhere on the main view back to the beginning of the screen uitapgesturerecognizer *tap = [[UITapGestureRecognizer alloc]initwithtarget:self action : @selEctor (TAP)];
   
[Self.view Addgesturerecognizer:tap];

 }

#pragma mark-Gesture recognition Method #define TARGETL-230 #define TARGETR #define SCREENW [UIScreen mainscreen].bounds.size.width-
   
  (void) Pan: (Uipangesturerecognizer *) Pan {//Get gesture movement position cgpoint TRANP = [Pan TranslationInView:self.view];
   
  Gets the offset of x cgfloat OffsetX = tranp.x;
   
  Modify the MAINV frame _mainv.frame = [self framewithoffsetx:offsetx];
   
  Determine if the MAINV x is greater than 0 [self observevalueforkeypath:nil ofobject:nil change:nil Context:nil];
   
  Reset [Pan Settranslation:cgpointzero InView:self.view];
     
    Determine when the gesture ends, position if (pan.state = = uigesturerecognizerstateended) {cgfloat target = 0;
    if (_mainv.frame.origin.x > Screenw * 0.5) {//positioned to the right target = TARGETR;
    }else if (Cgrectgetmaxx (_mainv.frame) < SCREENW * 0.5) {//positioned to the left target = Targetl;
     
    //Gets the offset that the X axis needs to move cgfloat OffsetX = target-_mainv.frame.origin.x; [UIView animatewithduration:0.25 animations:^{_mainv.frame = target = 0? seLf.view.bounds: [Self framewithoffsetx:offsetx];
     
  }];

 }
   
}

-(void) Tap {[UIView animatewithduration:0.25 animations:^{_mainv.frame = self.view.bounds;
   
}]; #define KMAXY #pragma mark-Calculates MAINV's frame-(CGRect) Framewithoffsetx by OffsetX: (cgfloat) OffsetX {//Get last Fram
   
  E CGRect frame = _mainv.frame;
   
  Gets the height of the screen cgfloat screenh = [UIScreen mainscreen].bounds.size.height;
   
  Gets the width of the screen//cgfloat screenw = [UIScreen mainscreen].bounds.size.width;
   
  X-axis translation point corresponding to the y-axis need to translate the distance cgfloat OffsetY = OffsetX * KMAXY/SCREENW;
   
  Gets the last height cgfloat preh = frame.size.height;
   
  Gets the last width cgfloat prew = frame.size.width;
  Get current height CGFloat curh = preH-2 * OffsetY;
  If you are sliding left if (frame.origin.x < 0) {Curh = Preh + 2 * offsetY;
   
  //Get the scaling scale of the dimension cgfloat scale = Curh/preh;
   
  Gets the current width cgfloat CURW = prew * scale;
   
  Gets the current x frame.origin.x + = OffsetX;
  Gets the current y cgfloat y = (screenh-curh)/2;
   
  FRAME.ORIGIN.Y = y;
  Frame.size.width = CURW; Frame.size.height = curH
   
return frame;

 }
-(void) Observevalueforkeypath: (NSString *) KeyPath Ofobject: (ID) object change: (nsdictionary<nsstring *,id> *) Change context: (void *) Context {
   
  if (_mainv.frame.origin.x > 0) {//sliding to the right
    _rightv.hidden = YES;
  } else if (_mainv.frame.origin.x < 0) {//slide to left
    _rightv.hidden = NO;
  }
   
}

If you want to display TableView in the MAINV main view, create a new Tableviewcontroller, where the TableView data is displayed,

-(Nsinteger) TableView: (UITableView *) TableView numberofrowsinsection: (nsinteger) Section {return
}
 
-(UITableViewCell *) TableView: (UITableView *) TableView Cellforrowatindexpath: (Nsindexpath *) IndexPath {
   
   
  static NSString *id = @ "cell";
   
  UITableViewCell *cell = [TableView dequeuereusablecellwithidentifier:id];
   
  if (cell = = nil) {
    cell = [[UITableViewCell alloc]initwithstyle:uitableviewcellstyledefault reuseidentifier:id];
  }
   
  Cell.textLabel.text = [NSString stringwithformat:@ "%ld rows", Indexpath.row];
   
  return cell;
}

Then create a controller Xxmainviewcontroller that is displayed in the storyboard, inherit from the Viewcontroller that implements the drawer effect, and change class to the class name of this control in storyboard. And the control of the display TableView as his sub controller.

-(void) viewdidload {
  [super viewdidload];
   
  Xxviewcontroller *VC = [[Xxviewcontroller alloc]init];
  Vc.view.frame = self.view.bounds;
   
  Let VC become the controller of the main view controller
  [self ADDCHILDVIEWCONTROLLER:VC];
   
  Main view display TableView
  [Self.mainv AddSubview:vc.view];
   
}

In order to access only MAINV in Xxmainviewcontroller and not modify his value, the view of the child control is exposed to ViewController.h and added read-only

#import <UIKit/UIKit.h>
 
@interface viewcontroller:uiviewcontroller
@property (nonatomic, weak, readonly) UIView *MAINV;
@property (nonatomic, weak, readonly) UIView *LEFTV;
@property (nonatomic, weak, readonly) UIView *rightv;
@end

Run Effect chart:

The above is the entire content of this article, I hope to learn about iOS program design help.

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.