您的位置:首页 > 移动开发 > IOS开发

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

2015-09-11 22:13 381 查看
#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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: