最简单的抽屉式原理 视图:左视图、右视图 左视图: 右视图:添加一个按钮,点击按钮,右移动 右视图:右滑视图,右移动 右视图:左滑视图,左移动,回到原位

#import "ViewController.h"

@interface ViewController (){

UIView * leftView;

UIView * rightView;

}

@end

@implementation ViewController

- (void)viewDidLoad {

[super viewDidLoad];

//左视图

leftView = [[UIView alloc]initWithFrame:self.view.frame];

//背景颜色

leftView.backgroundColor = [UIColor grayColor];

//加载

[self.view addSubview:leftView];

rightView = [[UIView alloc]initWithFrame:self.view.frame];

rightView.backgroundColor = [UIColor whiteColor];

[self.view addSubview:rightView];

//按钮

UIButton *theBtn = [UIButton buttonWithType:UIButtonTypeRoundedRect];

theBtn.frame=CGRectMake(40, 100, 40, 40);

[theBtn setTitle:@"按钮" forState:UIControlStateNormal];

[theBtn addTarget:self action:@selector(clickRight) forControlEvents:UIControlEventTouchUpInside];

//将按钮添加到右视图

[rightView addSubview:theBtn];

// 单击手势

//    UIGestureRecognizer  手势识别器 类

UITapGestureRecognizer * theTap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(clickTap)];

//加载到右视图

[rightView addGestureRecognizer:theTap];

//左右手势

UISwipeGestureRecognizer * theLeft = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(left)];

//滑动方向

//theLeft.direction = UISwipeGestureRecognizerDirectionLeft;

//加载到右视图

[rightView addGestureRecognizer:theLeft];

UISwipeGestureRecognizer * theRight = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(right)];

//滑动方向

//theRight.direction = UISwipeGestureRecognizerDirectionRight;

[rightView addGestureRecognizer:theRight];

}

//实现左右手势

-(void)left{

//改变右视图的frame

CGRect newFrame;

newFrame = rightView.frame;

newFrame.origin.x = 0;

//创建一个动画

[UIView beginAnimations:@"00" context:nil];

//设置动画总时长

[UIView setAnimationDuration:2];

//动画内容

rightView.frame = newFrame;

//提交动画

[UIView commitAnimations];

}

-(void)right{

//  改变右视图的frame

CGRect newFrame;

newFrame = rightView.frame;

newFrame.origin.x = self.view.frame.size.width - 100;

//  创建一个动画

[UIView beginAnimations:@"aa" context:nil];

//  设置动画总时长

[UIView setAnimationDuration:2.];

//  动画内容

rightView.frame = newFrame;

//  提交动画

[UIView commitAnimations];

}

-(void)clickRight{

//  改变右视图的frame

CGRect newFrame;

newFrame = rightView.frame;

newFrame.origin.x = self.view.frame.size.width - 100;

//  创建一个动画

[UIView beginAnimations:@"aa" context:nil];

//  设置动画总时长

[UIView setAnimationDuration:2.];

//  动画内容

rightView.frame = newFrame;

//  提交动画

[UIView commitAnimations];

}

-(void)clickTap{

//  改变右视图的frame

CGRect newFrame;

newFrame = rightView.frame;

newFrame.origin.x = 0;

//  创建一个动画

[UIView beginAnimations:@"aa" context:nil];

//  设置动画总时长

[UIView setAnimationDuration:2.];

//  动画内容

rightView.frame = newFrame;

//  提交动画

[UIView commitAnimations];

}

- (void)didReceiveMemoryWarning {

[super didReceiveMemoryWarning];

// Dispose of any resources that can be recreated.

}

@end


最简单的抽屉式原理 视图:左视图、右视图 左视图: 右视图:添加一个按钮,点击按钮,右移动 右视图:右滑视图,右移动 右视图:左滑视图,左移动,回到原位_第1张图片
最简单的抽屉式原理 视图:左视图、右视图 左视图: 右视图:添加一个按钮,点击按钮,右移动 右视图:右滑视图,右移动 右视图:左滑视图,左移动,回到原位_第2张图片

你可能感兴趣的:(最简单的抽屉式原理 视图:左视图、右视图 左视图: 右视图:添加一个按钮,点击按钮,右移动 右视图:右滑视图,右移动 右视图:左滑视图,左移动,回到原位)