iOS动画移动+复原 (用于类似抽屉)
2018-12-19 本文已影响0人
_YN
添加导航控制器 , 并且添加左按钮和右按钮 , 实现点击左按钮出现左视图 , 点击右按钮出现右视图功能
效果图



先创建对应的三个UIView
//一个主视图
@property (nonatomic , strong)UIView *MainView;
//左面的视图
@property (nonatomic , strong)UIView *LeftView;
//右面的视图
@property (nonatomic , strong)UIView *RightView;
添加左按钮和右按钮
//左按钮 , 点击左按钮的时候 , 会出现左视图
self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"left" style:UIBarButtonItemStyleDone target:self action:@selector(left)];
//右按钮 , 点击右按钮的时候 , 会出现右视图
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"right" style:UIBarButtonItemStyleDone target:self action:@selector(right)];
为三个视图设置位置和背景颜色 并且添加到视图
//分别为主视图、左视图、右视图设置位置
self.MainView = [[UIView alloc] initWithFrame:self.view.frame];
self.LeftView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 300, self.view.frame.size.height)];
self.RightView = [[UIView alloc] initWithFrame:CGRectMake(self.view.frame.size.width - 300, 0, 300, self.view.frame.size.height)];
//分别为主视图、左视图、右视图设置背景颜色 为了区分视图
self.MainView.backgroundColor = [UIColor whiteColor];
self.LeftView.backgroundColor = [UIColor redColor];
self.RightView.backgroundColor = [UIColor greenColor];
//添加视图 , 主视图要在最上面
[self.view addSubview:self.LeftView];
[self.view addSubview:self.RightView];
[self.view addSubview:self.MainView];
点击左按钮和右按钮的方法以及返回
//左按钮的点击方法
- (void)left{
//view 添加动画 设置为0.5秒完成
[UIView animateWithDuration:0.5 animations:^{
//除了左视图以外 所有视图向右移动 300
//导航控制器 x = 300 (向右移动300) y = 0 (向上移动0)
self.navigationController.navigationBar.transform = CGAffineTransformMakeTranslation(300, 0);
//_MainView x = 300 (向右移动300) y = 0 (向上移动0)
self->_MainView.transform = CGAffineTransformMakeTranslation(300, 0);
//_RightView x = 300 (向右移动300) y = 0 (向上移动0)
self->_RightView.transform = CGAffineTransformMakeTranslation(300, 0);
}];
}
- (void)right{
//view 添加动画 设置为0.5秒完成
[UIView animateWithDuration:0.5 animations:^{
//除了左视图以外 所有视图向右移动 300
//导航控制器 x = -300 (向右移动-300) y = 0 (向上移动0)
self.navigationController.navigationBar.transform = CGAffineTransformMakeTranslation(-300, 0);
//_MainView x = -300 (向右移动-300) y = 0 (向上移动0)
self->_MainView.transform = CGAffineTransformMakeTranslation(-300, 0);
//_LeftView x = -300 (向右移动-300) y = 0 (向上移动0)
self->_LeftView.transform = CGAffineTransformMakeTranslation(-300, 0);
}];
}
-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
//view 添加动画 设置为0.5秒完成
[UIView animateWithDuration:0.5 animations:^{
//导航控制器 复原
self.navigationController.navigationBar.transform = CGAffineTransformIdentity;
//_LeftView 复原
self->_LeftView.transform = CGAffineTransformIdentity;
//_MainView 复原
self->_MainView.transform = CGAffineTransformIdentity;
//_RightView 复原
self->_RightView.transform = CGAffineTransformIdentity;
}];
}