简单侧滑

来源:互联网 发布:淘宝联盟手机版5.2 编辑:程序博客网 时间:2024/05/01 09:21




@class LeftView;




@protocol LeftViewDelegate <NSObject>






- (void)LeftView:(LeftView *)view selectedIndex:(NSInteger)integer;


@end






@interface LeftView : UIView
<UITableViewDataSource, UITableViewDelegate>






@property (nonatomic, weak)   id<LeftViewDelegate>delegate;




@property (nonatomic, strong) NSArray *viewConttrollers;






- (id)initWithArray:(NSArray *)array;


@end


#import "LeftView.h"




#import <QuartzCore/QuartzCore.h>






@implementation LeftView






- (id)initWithArray:(NSArray *)array
{
  


  self = [super initWithFrame:CGRectMake(0, 0, 320, 480)];
  


  if (self) {
       


 self.backgroundColor=[UIColor colorWithRed:239.0/255 green:250.0/255 blue:233.0/255 alpha:1];
     


   self.viewConttrollers=array;
        
  
   
   UILabel *titleLabel_=[[UILabel alloc] initWithFrame:CGRectMake(0, 0, 260, 44)];
  


      titleLabel_.backgroundColor=[UIColor scrollViewTexturedBackgroundColor];
  


      titleLabel_.layer.shadowColor=[UIColor blackColor].CGColor;
        
titleLabel_.layer.shadowOffset=CGSizeMake(-2, -5);
        titleLabel_.layer.shadowOpacity=0.5;
    


    titleLabel_.text=@"此处可放置logo和一些按扭";
        
titleLabel_.font=[UIFont boldSystemFontOfSize:20];
//        
titleLabel_.textAlignment=NSTextAlignmentCenter;
        
titleLabel_.textColor=[UIColor whiteColor];
    




    [self addSubview:titleLabel_];
        
      


  UITableView *tableView_=[[UITableView alloc] initWithFrame:CGRectMake(0, 50, 260, 435)];
        
tableView_.backgroundColor=[UIColor clearColor];
        
tableView_.separatorStyle=UITableViewCellSeparatorStyleNone;
        
tableView_.rowHeight=50;
     


   tableView_.delegate=self;
      


  tableView_.dataSource=self;
      




  [self addSubview:tableView_];
        
     


   NSIndexPath *indexPath_=[NSIndexPath indexPathForRow:0 inSection:0];
     


    [tableView_ selectRowAtIndexPath:indexPath_ animated:YES scrollPosition:UITableViewScrollPositionNone];
    }
  


  return self;
}






- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
   


 return self.viewConttrollers.count;
}






- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
   


 static NSString *cellString_=@"tableCell";
    
  


  UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:cellString_];
  




  if (cell==nil) {
      


  cell=[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellString_];
        


cell.accessoryType=UITableViewCellAccessoryDisclosureIndicator;
    }
   


 UIViewController *viewController_=self.viewConttrollers[indexPath.row];
    
    
cell.imageView.image=viewController_.tabBarItem.image;
    
cell.textLabel.text=viewController_.tabBarItem.title;
    
   


 return cell;
}






- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
   


 if ([self.delegate respondsToSelector:@selector(LeftView:selectedIndex:)]) {
      


  [self.delegate LeftView:self selectedIndex:indexPath.row];
    }
}






- (void)btnClick{
    NSLog(@"left");
}


@end


#import "ViewController.h"




#import "OneViewController.h"




#import "TwoViewController.h"




#import "LeftView.h"






@interface ViewController ()<LeftViewDelegate>






@property (nonatomic, strong) UIView *transitionView;






@end


@implementation ViewController






- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
   


 self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        
    }
    return self;
}






- (void)viewDidLoad
{
    [super viewDidLoad];
   


 
    for (UIView *view_ in self.view.subviews) {
      


  if ([view_ isKindOfClass:[UITabBar class]]) {
         


   [view_ removeFromSuperview];
        }
    
   
 else{
        


    self.transitionView=view_;
        }
    }
    


self.transitionView.frame=CGRectMake(0, 0, 320, 480);
   


 
    //左划
  


  UISwipeGestureRecognizer *swipeLeftGesture_=[[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(swipeGesterClick:)];




    swipeLeftGesture_.direction=UISwipeGestureRecognizerDirectionLeft;//不设置默认为右
   


 [self.transitionView addGestureRecognizer:swipeLeftGesture_];




    swipeLeftGesture_=nil;


    //右划
   


 UISwipeGestureRecognizer *swipeRightGesture_=[[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(swipeGesterClick:)];
 


   [self.transitionView addGestureRecognizer:swipeRightGesture_];
    swipeRightGesture_=nil;
    


    


 OneViewController *oneViewController_=[[OneViewController alloc] init];
  


  UINavigationController *oneNav_=[[UINavigationController alloc] initWithRootViewController:oneViewController_];
    oneViewController_=nil;
    
  


  TwoViewController *twoViewController_=[[TwoViewController alloc] init];
   


 UINavigationController *twoNav_=[[UINavigationController alloc] initWithRootViewController:twoViewController_];
    twoViewController_=nil;
   


 
    self.viewControllers=@[oneNav_, twoNav_];
  


  oneNav_=nil;
   


 twoNav_=nil;
    




    LeftView *leftView_=[[LeftView alloc] initWithArray:self.viewControllers];
  


  leftView_.delegate=self;
   


 [self.view insertSubview:leftView_ atIndex:0];
  


  leftView_=nil;
    
   


 
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(showAndHiddenLeftView)
                                                 name:@"showAndHiddenLeftView"
                                               object:nil];
    
}






- (void)swipeGesterClick:(UISwipeGestureRecognizer *)swipeGesture{
    


float x=self.transitionView.frame.origin.x;


 


   if (swipeGesture.direction==UISwipeGestureRecognizerDirectionLeft && x>0) {
        x=0;
    


    [UIView beginAnimations:nil context:nil];
        
self.transitionView.frame=CGRectMake(x, 0, 320, 480);
    


    [UIView commitAnimations];
    }
   


 else if (swipeGesture.direction==UISwipeGestureRecognizerDirectionRight && x==0) {
      


  x=260;
      


  [UIView beginAnimations:nil context:nil];
        self.transitionView.frame=CGRectMake(x, 0, 320, 480);
  


      [UIView commitAnimations];
    }
    
}






- (void)showAndHiddenLeftView{
  


  float x=self.transitionView.frame.origin.x;
   


 if (x>0) {
        x=0;
    }
  


  else{
        x=260;
    }
   


 
    [UIView beginAnimations:nil context:nil];




    self.transitionView.frame=CGRectMake(x, 0, 320, 480);
  


  [UIView commitAnimations];
}






-(void)LeftView:(LeftView *)view selectedIndex:(NSInteger)integer{
    self.selectedIndex=integer;
}






- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


- (void)dealloc
{
    [[NSNotificationCenter defaultCenter] removeObserver:self name:@"showAndHiddenLeftView" object:nil];
}


@end