ios-侧滑-侧边栏 核心代码

88 篇文章 0 订阅
#import <UIKit/UIKit.h>
#import "MainViewController.h"
#import "LeftViewController.h"
#import "RightViewController.h"



@interface SliderViewController : UIViewController
{
    /**成员变量的声明*/
    MainViewController *_mainViewController;
    
    LeftViewController *_leftViewController;
    
    RightViewController *_rightViewController;
    
    /**背景视图*/
    UIImageView *_imageBackGround;
    
    //缩放比例
    CGFloat scalef;
}
/**
 *  滑动系数0.5-1之间
 */
@property(nonatomic,assign)CGFloat speed;

//是否允许点击视图恢复视图位置。默认为yes
@property (strong) UITapGestureRecognizer *sideslipTapGes;
/**
 *  初始化
 */
-(instancetype)initWithLeftView:(LeftViewController *)leftViewController andRightViewController:(RightViewController *)rightViewController andMainViewController:(MainViewController *)mainViewController andBackGroundImage:(UIImage *)image;
/**
 *  恢复位置
 */
-(void)showMainView;

/**
 *  <#Description#>
 */
-(void)showLeftView;
/**
 *  <#Description#>
 */
-(void)showRightView;
@end
#warning 这个程序存在bug 滑动会出现左右视图显示混乱

#import "SliderViewController.h"

@interface SliderViewController ()

@end

@implementation SliderViewController

@synthesize speed,sideslipTapGes;

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
}



#pragma mark -初始化视图-
//初始化
-(instancetype)initWithLeftView:(LeftViewController *)leftViewController andRightViewController:(RightViewController *)rightViewController andMainViewController:(MainViewController *)mainViewController andBackGroundImage:(UIImage *)image
{
    if (self = [super init]) {
        //初始化滑动系数
        speed = 0.5;
        _leftViewController = leftViewController;
        _rightViewController = rightViewController;
        _mainViewController = mainViewController;
       
        //设置背景图片
        UIImageView *imageView = [[UIImageView alloc]initWithFrame:[UIScreen mainScreen].bounds];
        [imageView setImage:image];
        [self.view addSubview:imageView];
        
        //添加手势到主视图控制器
        UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc]initWithTarget:self action:@selector(handlePan:)];
        [mainViewController.view addGestureRecognizer:pan];
        
        //添加单击手势到主视图控制器
        sideslipTapGes = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(handeTap:)];
        [mainViewController.view addGestureRecognizer:sideslipTapGes];
        
        
        //三个View 添加到本视图控制器并影藏其他两个
        leftViewController.view.hidden  = YES;
        rightViewController.view.hidden = YES;
        
        [self.view addSubview:leftViewController.view];
        [self.view addSubview:rightViewController.view];
        
        [self.view addSubview:mainViewController.view];
        
    }
    return self;
}
#pragma mark -滑动手势-

-(void)handlePan:(UIPanGestureRecognizer *)rec{
    //取得当前视图上的滑动点 移动距离
    CGPoint point = [rec translationInView:self.view];
    //缩放比例是按照滑动来的 即越往边上 视图越小
#warning mark 注意这里的缩放比例的计算
    scalef = (point.x*speed +scalef)
    ;
    NSLog(@"point.x:%f,speed:%f,scalef:%f",point.x,speed,scalef);
    
    //根据视图位置判断是左滑还是右滑
    if(rec.view.frame.origin.x >= 0)
    {
        //向右滑动
        NSLog(@">>>%f",rec.view.frame.origin.x);
        //位移
        //当前位置 = 上一位置+ 位移
        rec.view.center = CGPointMake(rec.view.center.x + point.x*speed, rec.view.center.y);
        //缩放
        /**
         *  首先缩放比要在0-1之间 
            scalf < point.x*speed<0
            经过试验 比上1000值刚好 然后在原用基础上缩小 用1减
         */
        
        rec.view.transform = CGAffineTransformScale(CGAffineTransformIdentity, 1-scalef/1000, 1-scalef/1000);
        
        //重新定位在坐标系中
        [rec setTranslation:CGPointMake(0, 0) inView:self.view];
        _rightViewController.view.hidden = YES;
        _leftViewController.view.hidden = NO;
        
    }
    else
    {
        rec.view.center = CGPointMake(rec.view.center.x + point.x*speed, rec.view.center.y);
        //缩放
        /**
         *  首先缩放比要在0-1之间
         scalf < point.x*speed<0
         经过试验 比上1000值刚好 然后在原用基础上缩小 用1减
         */
        
        rec.view.transform = CGAffineTransformScale(CGAffineTransformIdentity, 1+scalef/1000, 1+scalef/1000);
        
        //重新定位在坐标系中
        [rec setTranslation:CGPointMake(0, 0) inView:self.view];
        
        _rightViewController.view.hidden =NO;
        _leftViewController.view.hidden = YES;
    }
    
    //手势结束后修正位置
    if(rec.state == UIGestureRecognizerStateEnded)
    {
        //根据移动长度决定显示哪个视图
        if(scalef>140*speed){
            [self showLeftView];
        }
        else if(scalef <-140*speed){
            [self showRightView];
        }else{
            [self showMainView];
            scalef = 0;
        }
    }
    
}
#pragma mark -点击手势-
-(void)handeTap:(UIGestureRecognizer *)tap{
    
    if (tap.state == UIGestureRecognizerStateEnded) {
        [UIView beginAnimations:nil context:nil];
        tap.view.transform = CGAffineTransformScale(CGAffineTransformIdentity, 1.0, 1.0);
        tap.view.center = CGPointMake([UIScreen mainScreen].bounds.size.width/2, [UIScreen mainScreen].bounds.size.height/2);
        [UIView commitAnimations];
        //重置缩放比例
        scalef = 0;
    }
   
}



#pragma mark -修改视图位置
//恢复位置
-(void)showMainView
{
    [UIView beginAnimations:nil context:nil];
    _mainViewController.view.transform = CGAffineTransformScale(CGAffineTransformIdentity, 1,1 );//这里修改center的值
    _mainViewController.view.center = CGPointMake([UIScreen mainScreen].bounds.size.width/2, [UIScreen mainScreen].bounds.size.height/2);
    [UIView commitAnimations];
}

//显示左视图(主视图右移)
-(void)showLeftView
{
    [UIView beginAnimations:nil context:nil];
    _mainViewController.view.transform = CGAffineTransformScale(CGAffineTransformIdentity, 0.8, 0.8);
    _mainViewController.view.center = CGPointMake([UIScreen mainScreen].bounds.size.width+60 , [UIScreen mainScreen].bounds.size.height/2);
    [UIView commitAnimations];
}

//显示右视图(主视图左移动)
-(void)showRightView
{
    [UIView beginAnimations:nil context:nil];
    _mainViewController.view.transform = CGAffineTransformScale(CGAffineTransformIdentity, 0.8, 0.8);
    _mainViewController.view.center = CGPointMake(-60, [UIScreen mainScreen].bounds.size.height/2);
    [UIView commitAnimations];
}

-(BOOL)prefersStatusBarHidden
{
    return YES;
}

@end


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值