#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];
// 單擊手勢(shì)
//? ? UIGestureRecognizer? 手勢(shì)識(shí)別器 類
UITapGestureRecognizer * theTap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(clickTap)];
//加載到右視圖
[rightView addGestureRecognizer:theTap];
//左右手勢(shì)
UISwipeGestureRecognizer * theLeft = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(left)];
//滑動(dòng)方向
//theLeft.direction = UISwipeGestureRecognizerDirectionLeft;
//加載到右視圖
[rightView addGestureRecognizer:theLeft];
UISwipeGestureRecognizer * theRight = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(right)];
//滑動(dòng)方向
//theRight.direction = UISwipeGestureRecognizerDirectionRight;
[rightView addGestureRecognizer:theRight];
}
//實(shí)現(xiàn)左右手勢(shì)
-(void)left{
//改變右視圖的frame
CGRect newFrame;
newFrame = rightView.frame;
newFrame.origin.x = 0;
//創(chuàng)建一個(gè)動(dòng)畫
[UIView beginAnimations:@"00" context:nil];
//設(shè)置動(dòng)畫總時(shí)長
[UIView setAnimationDuration:2];
//動(dòng)畫內(nèi)容
rightView.frame = newFrame;
//提交動(dòng)畫
[UIView commitAnimations];
}
-(void)right{
//? 改變右視圖的frame
CGRect newFrame;
newFrame = rightView.frame;
newFrame.origin.x = self.view.frame.size.width - 100;
//? 創(chuàng)建一個(gè)動(dòng)畫
[UIView beginAnimations:@"aa" context:nil];
//? 設(shè)置動(dòng)畫總時(shí)長
[UIView setAnimationDuration:2.];
//? 動(dòng)畫內(nèi)容
rightView.frame = newFrame;
//? 提交動(dòng)畫
[UIView commitAnimations];
}
-(void)clickRight{
//? 改變右視圖的frame
CGRect newFrame;
newFrame = rightView.frame;
newFrame.origin.x = self.view.frame.size.width - 100;
//? 創(chuàng)建一個(gè)動(dòng)畫
[UIView beginAnimations:@"aa" context:nil];
//? 設(shè)置動(dòng)畫總時(shí)長
[UIView setAnimationDuration:2.];
//? 動(dòng)畫內(nèi)容
rightView.frame = newFrame;
//? 提交動(dòng)畫
[UIView commitAnimations];
}
-(void)clickTap{
//? 改變右視圖的frame
CGRect newFrame;
newFrame = rightView.frame;
newFrame.origin.x = 0;
//? 創(chuàng)建一個(gè)動(dòng)畫
[UIView beginAnimations:@"aa" context:nil];
//? 設(shè)置動(dòng)畫總時(shí)長
[UIView setAnimationDuration:2.];
//? 動(dòng)畫內(nèi)容
rightView.frame = newFrame;
//? 提交動(dòng)畫
[UIView commitAnimations];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end