首先更换根视图控制器
cmd+n 继承 UIViewController
在 AppDelegate 里 更改
1.前言:之前学习事件处理的时候学习过对屏幕的视图事件进行响应,主要通过的是几个方法,- touchesBegan等方法,为了更加方便的管理视图事件的处理,今天学习了UIGestureRecognizer类
2.定义:该类是一个抽象类,存放手势的公共的属性和方法,在iOS中一共有七大手势,轻拍、长按、轻扫、平移、捏合、旋转、屏幕边缘平移。
以上的几种手势都是继承自UIGestureRecognizer类的。该类有个特点,内部有一个view属性,存放的是接收该手势对象的视图。
GestureViewController *gesVC= [[GestureViewController alloc]init];
self.window .rootViewController = gesVC;
[gesVC release];
// 先建立一个视图 在视图中进行操作
UIView *view = [[UIView alloc]initWithFrame:CGRectMake(0, 30, 320, 400)];
view.backgroundColor = [UIColor randomColor];
[self.view addSubview:view];
[view release];
1.轻敲手势 UITapGestureRecognizer
//1.创建对象:
UITapGestureRecognizer *tapG = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(handleTapG:)];
//self ,指所以操作的视图,也就是视图控制器
//handleTapG: 一定要记得加冒号,这个是实现方法
//2.设置属性:
//数字代表轻敲几次才会发生事件 发生过一次之后 就会清除之前的 重新记录
tapG .numberOfTapsRequired = 1;
// 需要几个手指才能触发事件 下面的代码 是不需要之前那个复杂的提取代码的简化代码 可以直接使用
tapG.numberOfTouchesRequired = 2;
//3.添加到视图控制器的 view 上面
[view addGestureRecognizer:tapG];
[tapG release];
#pragma mark------轻击-----
-(void)handleTapG:(UITapGestureRecognizer *)view{
view.view.backgroundColor = [UIColor randomColor];
// 这个里面写的是方法 可以写任何你想在点击的时候触发什么情况
//方法 所以要写在外面
}
2.长按手势 UILongPressGestureRecognizer
//1.创建对象:
UILongPressGestureRecognizer *longPressG = [[UILongPressGestureRecognizer alloc]initWithTarget:self action:@selector(handleLongPressG:)];
//2.设置属性:
// 长按多久变色(响应事件) 最小时间
longPressG.minimumPressDuration = 1;
//3.添加到视图控制器的 view 上面
[view addGestureRecognizer:longPressG];
[longPressG release];
#pragma mark-------- 长按------
-(void)handleLongPressG:(UILongPressGestureRecognizer *)view{
// 注意:state是sender的一个属性,判断手势按压类型,不加判断默认值是所有手势类型
if(UIGestureRecognizerStateBegan == view.state){
view.view.backgroundColor = [UIColor redColor];
}
}
3.轻扫手势 UISwipeGestureRecognizer
//1.创建对象:
UISwipeGestureRecognizer *swipeG = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(handleSwipeG:)];
// 第二个对象 对应另一个方向的轻扫
UISwipeGestureRecognizer *swipeG1 = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(handleSwipeG:)];
// 2.属性
// 响应方式可以定义成一样 向上
swipeG1 .direction = UISwipeGestureRecognizerDirectionUp;
//3.给视图添加手势
[view addGestureRecognizer:swipeG1];
[view addGestureRecognizer:swipeG];
[swipeG release];
[swipeG1 release];
#pragma mark ---------实现轻扫方法-------
-(void)handleSwipeGesture:(UISwipeGestureRecognizer *)sender{
if (sender .direction == UISwipeGestureRecognizerDirectionLeft) {
//通过判断轻扫方向来确定响应的事件,为一个视图有多个轻扫方向而准备
sender .view.backgroundColor = [UIColor randomColor];
}else {
self.view.backgroundColor =[UIColor randomColor];
}
}
注:因为只能认一个方向 如果还想其他的方向 就要定义新的对象 但是可以用同一个手势实现方法
4.平移手势 UIPanGestureRecognizer
//1.创建对象
UIPanGestureRecognizer *panG= [[UIPanGestureRecognizer alloc]initWithTarget:self action:@selector(handlePanG:)];
// 2.给视图添加手势
[view addGestureRecognizer:panG];
[panG release];
#pragma mark-------平移-------
-(void)handlePanG:(UIPanGestureRecognizer *)view{
// 1.要考虑平移后的距离
CGPoint point = [view translationInView:view.view];
// 平移视图 point 的 x y 能控制你平移的方向 比如上下左右啊
// 2.修改视图的 transform 以最初的位置为基准 如果你动了 下次在动的时候 他会跳回之前的位置在动
view.view .transform = CGAffineTransformTranslate(view.view.transform, point.x, point.y) ;
//在原来视图的基础上,将x,y分别加上增量,以达到平移的效果,这种方法是将每一次的平移都叠加,所以效果不好,加上下面的方法
// 清理上次的增量 这样能保持不会越移越快
[view setTranslation:CGPointZero inView:view.view];
}
5.捏合手势(等比缩放) UIPinchGestureRecognizer
//1.创建对象:
UIPinchGestureRecognizer *pinchG = [[UIPinchGestureRecognizer alloc]initWithTarget:self action:@selector(handlePinchG:)];
//2.为视图添加手势
[view addGestureRecognizer:pinchG];
[pinchG release];
#pragma mark------捏合事件-------
-(void)handlePinchG:(UIPinchGestureRecognizer *)view{
//捏合就是等比缩放 缩放手势对象有一个实例变量交scale,指的是缩放比例
// 1.获得改变的大小 transform
view.view.transform = CGAffineTransformScale(view.view.transform, view.scale, view.scale);
//在原来视图的基础上,x,y都缩放scale,这个也是累加,所以也需要下面的一句
view.scale = 1;
}
6.旋转手势 UIRotationGestureRecognizer
//1.创建对象:
UIRotationGestureRecognizer *rotationG = [[UIRotationGestureRecognizer alloc]initWithTarget:self action:@selector(handleRotationG:)];
//2.为视图添加手势
[view addGestureRecognizer:rotationG];
[rotationG release];
#pragma mark---------旋转-------
-(void)handleRotationG:(UIRotationGestureRecognizer *)view{
//Rotation 是弧度,
view.view.transform = CGAffineTransformRotate(view.view.transform, view.rotation);
//清零 保证不会让视图越转越快
view.rotation = 0.0;
}
7.// 屏幕边缘移动 UIScreenEdgePanGestureRecognizer
这是一个比较特殊的手势 他继承的是 UIPanGestureRecognizer 也就是平移手势
//1.创建对象:
UIScreenEdgePanGestureRecognizer *screenG = [[UIScreenEdgePanGestureRecognizer alloc]initWithTarget:self action:@selector(handleScreenG:)];
//2.为视图添加手势 这个视图必须紧挨屏幕边缘
[view addGestureRecognizer:screenG];
//因为默认的是没有移动方向 所以我们需要自己设定他往那边移动
screenG .edges = UIRectEdgeLeft;// 从左侧触发
[screenG release];
#pragma mark ------- 屏幕边缘 -----
-(void)handleScreenG:(UIScreenEdgePanGestureRecognizer *)view{
// 1. 还是要先求平移的 增量
CGPoint point = [view translationInView:view.view];
// 2. 平移视图 左右平移 所以 point.y 是没有值的
view.view.transform = CGAffineTransformTranslate(view.view.transform, point.x, 0);
// 3.平移的距离也是叠加的,所以要在每一次平移之后将平移增量赋值为0
[view setTranslation:CGPointZero inView:view.view];
}
小知识点:当屏幕边缘平移之后 我们可以通过 botton 将视图在回来
//1.创建一个 BUTTON:
UIButton *button = [UIButton buttonWithType:(UIButtonTypeSystem)];
//2.定义属性
button.frame = CGRectMake(10, 480, 300, 30);
[button setTitle:@"回来" forState:UIControlStateNormal];
button.backgroundColor = [UIColor cyanColor];
//3.创建响应时间
[button addTarget:self action:@selector(back:) forControlEvents:UIControlEventTouchUpInside];
//4.添加到视图内
[self.view addSubview:button];
#pragma mark---------BUTTON-----
-(void)back:(UIButton *)sender{
// sender 是一个形参
//[sender.superView viewWithTag:100]; 就是这个参数贴到的父视图 上面有一个 tag 值为100的视图 从而找到并使用 redView 我们在这里是不能直接使用 redView 的
UIView *view = [sender.superview viewWithTag:100];
view.frame = CGRectMake(0 , 30 , 260 , 400);
NSLog(@"_____");//检测 是否走这个方法了没有
//view.center = CGPointMake(160, 180);//这种方式是无法回复原样的
}
总结:
1.对于手势 主要是记住 UI 后面的词 ,除去这个词 后面的都一样
2.如果想看自己程序有没有走 可以在方法里面加上打印 NSLog(@”++++++”);
3.屏幕边缘手势继承平移手势 轻扫和边缘移动 设定方向需要从新定义对象