#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