zoukankan      html  css  js  c++  java
  • 手势

    UIkit框架中绝大多数的控件都是继承自,UIResponder类,UIResponder 类有强大的处理触摸事件的能力。假如一个UIview 收到一个触摸事件,那么这个触摸事件就会去进行寻找相应的响应事件,如果在该UIview 中找不到,就寻找UIView的对象去处理,如果UIView对象没有权利处理,就往当前的上一层UIViewController去寻找,如果找不到就再寻找 UIViewController 的对象去处理,如果这个对象仍然不能处理,就再往上层 UIWindow 对象去处理,如果它热不能解决触摸事件的响应,那该触摸事件就会被传递到 UIApplication 代理对象,如果该代理对象仍不能解决,那就交给系统回收。

    总结一下:相当于在村里发生一件事,村长不能决定,这时就一级一级上报,可是一直没有得到处理,直到最后有个很大权力的人拍板决定,才算结束。要不就一直往上报。

    系统将事件封装到 UIEvent 类中,然后由系统去处理。ios 将事件分为三种:触摸事件、动作事件、外部控制事件。动作事件:就是用户对手机进行的特定的动做,比如摇一摇;外部控制事件:就是控制手机连上手机设备时候的事件;触摸事件:就是用户与手机屏幕的相关事件。

    每一个用户交互对象 UIResponder 都有一组响应事件函数。通常我们都要重写这组函数。以供我们使用相应的逻辑。

    关于概念的知识 参考

    代码实现功能:打印出鼠标的手势事件

    建一个 UIView 的文件命名为 TouchView 在视图控制器里写上

    #import "RootViewController.h"
    #import "TouchView.h"
    
    
    @interface RootViewController ()
    
    @end
    
    @implementation RootViewController
    
    - (void)viewDidLoad {
        [super viewDidLoad];
        [self setTouchView];
    }
    
    -(void)setTouchView{
        TouchView * touchView = [[TouchView alloc]initWithFrame:CGRectMake(50, 50, 100, 100)];
        touchView.backgroundColor = [UIColor redColor];
        [self.view addSubview:touchView];
        
        UIButton * Button = [UIButton buttonWithType:UIButtonTypeCustom];
        Button.frame = CGRectMake(20, 200, 280, 30);
        Button.backgroundColor = [UIColor grayColor];
        [Button addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
        [Button setTitle:@"点击跳转" forState:UIControlStateNormal];
        [self.view addSubview:Button];
        
        pinchView * View = [[pinchView alloc]initWithFrame:CGRectMake(50, 300, 100, 100)];
        View.backgroundColor = [UIColor blackColor];
        [self.view addSubview:View];
        
        
    }
    
    -(void)buttonAction:(UIButton *)sender{
        SecondViewController * SVC = [[SecondViewController alloc]init];
        [self.navigationController pushViewController:SVC animated:YES];
    }
    
    - (void)didReceiveMemoryWarning {
        [super didReceiveMemoryWarning];
        // Dispose of any resources that can be recreated.
    }

    在 UIView.m文件里写上

    #import "TouchView.h"
    
    @implementation TouchView
    
    -(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
        [self updateInfor:[touches anyObject] withMethodName:@"touchesBegin"];
    }
    -(void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event{
        [self updateInfor:[touches anyObject] withMethodName:@"touchesCancelled"];
    }
    -(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event{
        [self updateInfor:[touches anyObject] withMethodName:@"touchesEnded"];
    }
    -(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event{
        [self updateInfor:[touches anyObject] withMethodName:@"touchesMoved"];
    }
    
    -(void)updateInfor:(UITouch *)aTouch withMethodName:(NSString *)aMethodName{
        NSString * strPhase = @"";
        switch (aTouch.phase) {
            case UITouchPhaseBegan:
                strPhase = @"UITouchPhaseBegan";
                break;
            case UITouchPhaseEnded:
                strPhase = @"UITouchPhaseEnded";
                break;
            case UITouchPhaseCancelled:
                strPhase = @"UITouchPhaseCancelled";
                break;
            case UITouchPhaseMoved:
                strPhase = @"UITouchPhaseMoved";
                break;
            default:
                break;
        }
        NSLog(@"操作事件是 %@",strPhase);
    }
    @end

     触摸事件冲突,eg: 在一个有 UITableView  的页面,在view 上添加一个手势,要实现轻拍 非 UITableView 的地方关闭页面,这时候发现点击了UITableVIew 也会关闭该页面,因为时间的传递导致了改结果,如何解决,只需要实现下面的手势协议即可,协议内部代码如下:

    -(BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch {
        if ([NSStringFromClass([touch.view class]) isEqualToString:@"UITableViewCellContentView"]) {
            return NO;
        }
        return YES;
    }
    
  • 相关阅读:
    js中let和var定义变量的区别
    windows下开发PHP扩展dll(无需Cygwin)
    用VS开发PHP扩展
    破解电信光猫华为HG8120C关闭路由功能方法
    从程序员到项目经理(二十九):怎样写文档
    从程序员到项目经理(二十八):该死的结果导向(只看结果,不问过程到底行不行?)
    从程序员到项目经理(二十七):怎样给领导汇报工作
    从程序员到项目经理(二十六):项目管理不能浑水摸鱼
    从程序员到项目经理(二十五):对绩效考核的吐槽
    从程序员到项目经理(二十四):慎于问敏于行
  • 原文地址:https://www.cnblogs.com/benpaobadaniu/p/4926227.html
Copyright © 2011-2022 走看看