zoukankan      html  css  js  c++  java
  • [4]事件处理

    事件处理

    UIEvent:事件,是由硬件捕捉的一个表示用户操作设备的对象
    

    触摸事件

    触摸事件:用户通过触摸设备屏幕操作对象、输入数据。支持多点 触摸,包含1个到多个触摸点
    

    触摸开始执行的方法

    //touches:手指个数  envet:事件
    - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
    {
        //大小随机
        CGFloat x = arc4random() % 276;
        CGFloat y = arc4random() % 568;
        CGFloat width = arc4random() % (375 -10) - x + 10;
        CGFloat height = arc4random() % (667 - 10) - y + 10;
        self.frame = CGRectMake(x, y, width, height);
        //颜色随机
        CGFloat f = arc4random()%256/255.0;
        self.backgroundColor = [UIColor colorWithRed:f green:f blue:f alpha:1.0];
        NSLog(@"%s,%d",__FUNCTION__,__LINE__);
    }
    

    触摸移动过程中执行的方法

    //实现色块随着鼠标拖动
    - (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
    {
        //从事件中获取当前实践中的所有手指
        NSSet *alltouches = [event touchesForView:self];
        //    NSLog(@"%@",alltouches);
        
        //此时,alltouches与参数中detouches其实内容是一样的.
        UITouch *touch = [alltouches anyObject];
    //    NSLog(@"%@",touch);
        
        //获取当前这个点在视图中坐标位置
        CGPoint p = [touch locationInView:self];
        
        //获取拖动之前的坐标位置
        CGPoint p1 = [touch previousLocationInView:self];
        
        //通过改变Center来改变视图的显示位置
    //    CGPoint newCenter;
    //    newCenter.x = self.center.x + (p.x - p1.x);
    //    newCenter.y = self.center.y + (p.y - p1.y);
    //    self.center = newCenter;
        
        //通过frame来改变视图的显示位置
        CGRect newFrame = self.frame;
        newFrame.origin.x = self.frame.origin.x + (p.x - p1.x);
        newFrame.origin.y = self.frame.origin.y + (p.y - p1.y);
        self.frame = newFrame;
        NSLog(@"%s,%d",__FUNCTION__,__LINE__);
    }
    

    结束触摸执行的方法

    -(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
    {
        NSLog(@"%s,%d",__FUNCTION__,__LINE__);
    }
    

    阻断触摸时触发的方法

    -(void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event
    {
        NSLog(@"%s,%d",__FUNCTION__,__LINE__);
    }    
    

    总结

    我写了一个视图控制器,控制的视图中有三个子视图
    1视图:点击颜色变化 2视图:拖拽 3视图:变化大小

    
        CGPoint p = [touch locationInView:self];
        CGPoint p1 = [touch locationInView:self.test1];
        CGPoint p3 = [touch locationInView:self.test3];
        NSLog(@"@00@@%lf,%lf",p.x,p.y);
        NSLog(@"11$$%lf,%lf",p1.x,p1.y);
        NSLog(@"33$$%lf,%lf",p3.x,p3.y);
    

    得到的结论是 点击到色块的时候输出为0,可以通过这个加Center实现每个色块单独起来

    晃动事件

    暂无介绍

    远程控制事件

    暂无介绍

    响应者链

    UIResponder 响应者类
    hit-test 点击检测

    UIGestureRecognizer 手指触控的衍生类

    在 iPhone 或 iPad 的开发中,除了用 touchesBegan / touchesMoved / touchesEnded 这组方法来控制使用者的手指触控外,也可以用 UIGestureRecognizer 的衍生类別来进行判断。用 UIGestureRecognizer 的好处在于有现成的手势,开发者不用自己计算手指移动轨迹。UIGestureRecognizer的衍生类別有以下几种:
    UITapGestureRecognizer //Tap(点一下)
    UIPinchGestureRecognizer //Pinch(二指往內或往外拨动)
    UIRotationGestureRecognizer //Rotation(旋转)
    UISwipeGestureRecognizer //Swipe(滑动,快速移动)
    UIPanGestureRecognizer //Pan (拖移,慢速移动
    UILongPressGestureRecognizer //LongPress(长按)
    从命名上不难了解這些类別所对应代表的手势,分別是 Tap(点一下)、Pinch(二指往內或往外拨动)、Rotation(旋转)、Swipe(滑动,快速移动)、Pan (拖移,慢速移动)以及 LongPress(长按)。這些手势別在使用上也很简单,只要在使用前定义并添加到对应的视图上即可。

        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tap)];
        tap.numberOfTapsRequired = 1;  //点击数 单击  双击 
        tap.numberOfTouchesRequired = 1; //手指数 多手触摸
        [_loginView addGestureRecognizer:tap];
        [tap release];
    

    小知识

    UIImageView UILabel userInteractionEnabled默认交互是关闭的 其他是默认开启的

    On the road。。。
  • 相关阅读:
    linux常用命令
    10.8统计英文词频
    9月10号作业
    华氏温度与摄氏温度转换
    小故事
    Java的工厂模式(三)
    Javascript实现图片翻转
    Java的工厂模式(二)
    Java的工厂模式(一)
    Java新建线程的两种方式
  • 原文地址:https://www.cnblogs.com/ianhao/p/4452061.html
Copyright © 2011-2022 走看看