博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
手势。。。
阅读量:5014 次
发布时间:2019-06-12

本文共 4274 字,大约阅读时间需要 14 分钟。

UITouch

1.UITouch *touch = [touches anyObject];//找到触摸事件

   if([touch.view isKindOfClass:[UIImageView class]]){

              CGPoint point = [touch locationInView:self.view];//找到点击事件在self.view里的坐标

              CGFloat xMove = point.x - _recordPoint.x;

              CGFloat yMove = point.y - _recordPoint.y;//计算运动轨迹

      touch.view.center = CGPointMake(touch.view.center.x +xMove,touch.view.center.y + yMove);//根据运动轨迹移动到点击到的视图

      _recordPoint = [touch locationInView:self.view];//每次移动以后都修改记录

2.根据点击的次数来监听

   - (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event

     {

            UITouch *touch = [touches anyObject];

    
                NSLog(@"====%ld",touch.tapCount);
               //连续点击的次数
    
                 if ([touch.view isKindOfClass:[UIImageView class]]) {
                if (touch.tapCount == 2) {
            CGRect rect = touch.view.frame;
            rect.origin.x -= 5;
            rect.size.width += 10;
            touch.view.frame = rect;
            //修改frame
        }
    }
}

 

各种手势

1.点击手势(类似于btn的点击)

       UITapGestureRecognizer *tapGR = [[UITapgestureRecognizer alloc] initWithTarget:self action:@selector(tapGR:)];

       [view addGestureRecognizer:tapGR];

       - (void)tapGR:(UITapGestureRecognizer *)tapGR

         {

            [self.view bringSubViewToFront:tapGR.view];//将手势对应的view移到最上层

         }

2.移动手势

        UIPanGestureRecognizer *panGR = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(panGR:)];

        [view addGestureRecognizer:panGR];

1 - (void)panGR:(UIPanGestureRecognizer *)panGR 2 { 3     CGPoint translation = [panGR translationInView:self.view]; 4     //获取手势移动的轨迹 5      6     panGR.view.center = CGPointMake(panGR.view.center.x + translation.x, panGR.view.center.y + translation.y); 7     //根据手势的轨迹移动view 8      9     [panGR setTranslation:CGPointZero inView:self.view];10     //每次移动以后都清空手势记录的轨迹11 }

 

       

3.捏合手势

        UIPinchGestureRecognizer *pinchGR = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(pinchGR:)];

         [view addGestureRecognizer:pinchGR];

   

1 - (void)pinchGR:(UIPinchGestureRecognizer *)pinchGR2 {3     pinchGR.view.transform = CGAffineTransformScale(pinchGR.view.transform, pinchGR.scale, pinchGR.scale);4     //让view随着手势的缩放而缩放5     6     pinchGR.scale = 1;7     //缩放以后需要还原手势中记录的倍数8 }

 

4.旋转手势

        UIRotationGestureRecognizer *rotationGR = [[UIRotationGestureRecognizer alloc] initWithTarget:self action:@selector(rotationGR:)];

    [view addGestureRecognizer:rotationGR];

 

1 - (void)rotationGR:(UIRotationGestureRecognizer *)rotationGR2 {3     rotationGR.view.transform = CGAffineTransformRotate(rotationGR.view.transform, rotationGR.rotation);4     //让view随着手势的旋转而旋转5     6     rotationGR.rotation = 0;7     //旋转以后还原记录8 }

 

5.长按手势

       UILongPressGestureRecognizer *longGR = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longGR:)];

    [view addGestureRecognizer:longGR];

   

1 - (void)longGR:(UILongPressGestureRecognizer *)longGR 2 { 3     NSLog(@"===%ld",longGR.state); 4     //打印手势的状态(开始响应,改变,结束响应) 5      6     if (longGR.state == UIGestureRecognizerStateBegan) { 7         //长按手势开始响应时触发 8          9         CABasicAnimation *basicAnimation = [CABasicAnimation animationWithKeyPath:@"transform.scale"];10         //创建一个基础动画11         12         basicAnimation.fromValue = [NSNumber numberWithFloat:0.5];13         basicAnimation.toValue = @2;14         //从哪里来,到哪里去15         16         basicAnimation.autoreverses = YES;17         //是否以动画方式返回18         19         basicAnimation.duration = 0.1;20         //单次动画时间21         22         basicAnimation.repeatCount = 99;23         //重复次数24         25         [longGR.view.layer addAnimation:basicAnimation forKey:@"aa"];26         //添加动画27     }28 }

 

6.轻扫手势(和移动手势有冲突)

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

        //轻扫手势(和pan移动有冲突的)
         swipeGR.direction = UISwipeGestureRecognizerDirectionUp;
        //设置轻扫的方向
         [view addGestureRecognizer:swipeGR];

        如果想实现多个方向的轻扫手势,需要创建多个,然后设置不同的方向。。。。。

       

1 - (void)swipeGR:(UISwipeGestureRecognizer *)swipeGR 2 { 3     CGRect rect = swipeGR.view.frame; 4      5     //判断方向 6     if (swipeGR.direction == UISwipeGestureRecognizerDirectionUp) { 7         rect.origin.y -= 5; 8         rect.size.height += 5; 9     }10     11     //& 按位与判断(这种用法只能是枚举值后面有1<<2这种位操作的)12     if (swipeGR.direction & UISwipeGestureRecognizerDirectionLeft) {13         rect.origin.x -= 5;14         rect.size.width += 5;15     }16     17     swipeGR.view.frame = rect;18 }

 

转载于:https://www.cnblogs.com/Angelone/p/4388209.html

你可能感兴趣的文章
Mysql的read_only 只读属性说明 (运维笔记)
查看>>
DOCKER 从入门到放弃(五)
查看>>
Python 多线程学习
查看>>
appcan官方ajax
查看>>
获取NVIDIA显卡的温度
查看>>
Dijkstra算法
查看>>
Deep Learning 9: Performance
查看>>
面试题61 把二叉树打印成多行
查看>>
C#例子 易懂故事 接口 委托 事件 异步通知 好玩.
查看>>
[转]Windows Shell 编程 第十一章 【来源:http://blog.csdn.net/wangqiulin123456/article/details/7987992】...
查看>>
修改presto新版源码让他支持redash数据库
查看>>
Javascript的书写位置
查看>>
树-线索二叉树
查看>>
JAVA遇见HTML——Servlet篇:Servlet基础
查看>>
第二章 Vue快速入门--20 品牌案例-完成品牌列表的添加功能+ 21 品牌案例-根据Id完成品牌的删除...
查看>>
Java单例模式
查看>>
重温WCF之消息契约(MessageContract)(六)
查看>>
Excel2007制作直方图和正态分布曲线图
查看>>
android adb常用指令
查看>>
Android框架之路——GreenDao3.2.2的使用
查看>>