Drawer effect Rewriting

Source: Internet
Author: User

Drawer effect Rewriting

// This is the content of the main interface. You can drag the main interface to achieve the effect. // MainViewController. m // WangYiDrawer /// Created by xiaoyao on 15/2/9. // Copyright (c) 2015 lijien. all rights reserved. // # import "MainViewController. h "# import" LeftViewController. h "# import" RightViewController. h "# import
 
  
# Define kEndX frame. origin. x // x # define kLeftViewWidth _ leftView after the gesture ends. frame. size. width // The width of the Left view # define kRightViewWidth _ rightView. frame. size. width // The width of the right view @ interface MainViewController ()
  
   
{CGFloat _ startX; // start point LeftViewController * _ leftViewController when the gesture is triggered; // left View Controller RightViewController * _ rightViewController; // right View Controller NSMutableDictionary * _ columnViewControllers; // record all the instantiated controller objects to prevent repeated creation of UIView * _ mainView;} @ end @ implementation MainViewController-(void) viewDidLoad {[super viewDidLoad]; _ mainView. frame = self. view. bounds; [self. view addSubview: _ mainView]; _ titleButton. backgroundColor = [UIColor clearColor]; _ columnViewControllers = [[NSMutableDictionary alloc] init]; // Add the view of the left-side view controller to the left-side view _ leftViewController = [[LeftViewController alloc] init]; _ leftViewController. view. frame = _ leftView. frame; _ leftViewController. delegate = self; [_ leftView addSubview: _ leftViewController. view]; // Add the view of the right-side view controller to the left-side view _ rightViewController = [[RightViewController alloc] init]; _ rightViewController. view. frame = _ rightView. frame; [_ rightView addSubview: _ rightViewController. view]; UIPanGestureRecognizer * pan = [[UIPanGestureRecognizer alloc] initWithTarget: self action: @ selector (panGesture :)]; [self. view addGestureRecognizer: pan]; // The news content [self firstLoading] should be displayed during the first loading;} // hide the status bar-(BOOL) prefersStatusBarHidden {return YES ;} -(void) firstLoading {Column * column = [Column columnName: @ "news" imgName: @ "news.png" className: @ "NewsViewController"]; [self leftTableViewRowClicked: column];} -(void) panGesture :( UIPanGestureRecognizer *) pan {// if the status is just getting started, record the current starting x if (UIGestureRecognizerStateBegan = pan. state) {_ startX = self. view. frame. origin. x;} // calculates the distance between the pan dragging and CGPoint delta = [pan translationInView: self. view]; CGRect frame = _ mainView. frame; kEndX = _ startX + delta. x; // restrict the drag range if (kEndX> = kLeftViewWidth) {kEndX = kLeftViewWidth;} if (kEndX <=-kRightViewWidth) {kEndX =-kRightViewWidth ;} // because leftView and rightView overlap, you must hide an if (kEndX> 0) {_ rightView. hidden = YES; _ leftView. hidden = NO;} else {_ rightView. hidden = NO; _ leftView. hidden = YES;} if (UIGestureRecognizerStateEnded = pan. state) {if (_ startX = 0 & delta. x> 0) {kEndX = kLeftViewWidth;} else if (_ startX = 0 & delta. x <0) {kEndX =-kRightViewWidth;} else if (_ startX = kLeftViewWidth & delta. x <0) {kEndX = 0;} else if (_ startX =-kRightViewWidth & delta. x> 0) {kEndX = 0 ;}// re-set mainView frame [UIView animateWithDuration: 0.2 animations: ^ {_ mainView. frame = frame;}]; // Add a shadow layer [self addShadowForMianViewWithEndX: kEndX];}-(void) addShadowForMianViewWithEndX :( CGFloat) endX {_ mainView. layer. shadowColor = [UIColor blackColor]. CGColor; _ mainView. layer. shadowOpacity = 0.5; if (endX> 0) {_ mainView. layer. shadowOffset = CGSizeMake (-5, 0);} else {_ mainView. layer. shadowOffset = CGSizeMake (5, 0) ;}# pragma mark-leftViewDelegate-(void) leftTableViewRowClicked :( Column *) rowSelected {Column * column = (Column *) rowSelected; // 1, close the control on the left ========================/// call the extracted public code, set x of mainView. The parameter is endX [self setmainViewX: 0]; // 2. Change the text _ titleButton above the title button. titleLabel. text = column. columnName; // instantiate the controller of the corresponding topic based on the class name in the topic data model, set it as the root controller of the navigation controller, and add the view of the navigation controller to mainView, the purpose is to conveniently set the navigation bar, as well as jump to/2 for each controller, from the cache dictionary. If the sub-controller dictionary has a previously created sub-controller, use UIViewController * columnVC = _ columnViewControllers [column. columnClassName]; // create a subcontroller if (columnVC = nil) {Class c = NSClassFromString (column. columnClassName); columnVC = [[c alloc] init]; // you must put it in the sub-controller dictionary and store it [_ columnViewControllers setObject: columnVC forKey: column. columnClassName];} // 4, remove the existing sub-view if (_ contentView. subviews. count> 0) {UIView * oldView = [_ contentView subviews] [0]; [oldView removeFromSuperview];} // 5. Add the view of the subcontroller to contentView, show columnVC. view. frame = _ contentView. bounds; [self. contentView addSubview: columnVC. view]; NSLog (@ "% @", self. contentView); // obtain the subcontroller of the mainView navigation controller and remove it (if any) before adding it to the mainView. // extract the public code, set the x of mainView. The parameter is endX-(void) setmainViewX :( CGFloat) endX {CGRect frame = _ mainView. frame; frame. origin. x = endX; [UIView animateWithDuration: 0.2 animations: ^ {_ mainView. frame = frame;}];} @ end/*************** set proxy in the left-side table ******** // LeftViewController. m // WangYiDrawer /// Created by xiaoyao on 15/2/9. // Copyright (c) 2015 lijien. all rights reserved. // # import "LeftViewController. h "@ interface LeftViewController () {NSArray * _ classNameArray; UITableView * _ tableView;} @ end @ implementation LeftViewController-(void) viewDidLoad {[super viewDidLoad]; _ tableView = [[UITableView alloc] initWithFrame: self. view. bounds]; [self. view addSubview: _ tableView]; NSArray * titleArray = @ [@ "news", @ "image", @ ""]; NSArray * picArray = @ [@ "news.png ", @ "pic.png", @ "comment.png"]; Column * newsColumn = [Column columnName: titleArray [0] imgName: picArray [0] className: @ "NewsViewController"]; column * picColumn = [Column columnName: titleArray [1] imgName: picArray [1] className: @ "PicViewController"]; Column * commentColumn = [Column columnName: titleArray [2] imgName: picArray [2] className: @ "CommentViewController"]; // Add the created controller object to the array for management. // _ className = [NSArray arrayWithObjects: newsColumn, picColumn, commentColumn, nil]; _ classNameArray = @ [newsColumn, picColumn, commentColumn];} # pragma mark-UITableViewDelegate-(NSInteger) tableView :( UITableView *) tableView numberOfRowsInSection :( NSInteger) section {return _ classNameArray. count;}-(CGFloat) tableView :( UITableView *) tableView heightForRowAtIndexPath :( NSIndexPath *) indexPath {return 40;} # pragma mark-UITableViewDataSource-(UITableViewCell *) tableView :( UITableView *) tableView cellForRowAtIndexPath :( NSIndexPath *) indexPath {static NSString * cellIde = @ "cellIde"; UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier: cellIde]; if (cell = nil) {cell = [[UITableViewCell alloc] initWithStyle: UITableViewCellStyleSubtitle reuseIdentifier: cellIde];} if (indexPath. row <_ classNameArray. count) {Column * column = [_ classNameArray objectAtIndexedSubscript: indexPath. row]; cell. textLabel. text = column. columnName; cell. imageView. image = [UIImage imageNamed: column. columnImgName];} return cell;}-(void) tableView :( UITableView *) tableView didSelectRowAtIndexPath :( NSIndexPath *) indexPath {// cancel the highlighted state of each row [tableView deselectRowAtIndexPath: indexPath animated: YES]; Column * column = nil; if (indexPath. row <_ classNameArray. count) {column = [_ classNameArray objectAtIndexedSubscript: indexPath. row];} if ([self. delegate respondsToSelector: @ selector (leftTableViewRowClicked :)]) {[self. delegate leftTableViewRowClicked: column] ;}}@ end
  
 

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.