zoukankan      html  css  js  c++  java
  • 解析iOS开发中的FirstResponder第一响应对象

    1. UIResonder

    对于C#里所有的控件(例如TextBox),都继承于Control类。而Control类的继承关系如
    下:

    代码如下:

    System.Object
     
    
      System.MarshalByRefObject
    
        System.ComponentModel.Component
    
          System.Windows.Forms.Control
    对于iOS里的UI类,也有类似的继承关系。

    例如对于UITextField,继承于UIControl;UIControl继承于UIView,UIView继承于UIRe
    sponder,UIResponder继承于NSObject。

    具体架构可以参见:

    http://developer.apple.com/library/ios/#documentation/general/conceptual/Devp
    edia-CocoaApp/Responder.html



    UIResponder是UIKit框架中的类(Mac OS X Cocoa对应的是AppKit框架)。

    2. 第一响应对象

    在应用的响应对象里,会有一个成为第一响应对象。

    第一响应对象和其他响应对象之间有什么区别?对于普通的触摸事件没什么区别。就算
    我把一个按钮设置成第一响应对象,当我点击其他按钮时,还是会响应其他按钮,而不
    会优先响应第一响应对象。

    第一响应对象的区别在于负责处理那些和屏幕位置无关的事件,例如摇动。

    苹果官方文档的说法是:第一响应对象是窗口中,应用程序认为最适合处理事件的对象

    一个班只能有一个班长,应用的响应对象中,只能有一个响应对象成为第一响应对象。

    3. 成为与取消第一响应对象。

    要当第一响应对象,还需要有View来毛遂自荐:

    代码如下:

    - (BOOL) canBecomeFirstResponder
    {
        returnYES;
    }

    如果缺少了这段,就算用[view becomeFirstResponder]也不能让一个view成为第一响应

    对象。。。强扭的瓜不甜?好吧不是这个原因。大多数视图默认只关心与自己有关联的
    事件,并且(几乎)总是有机会来处理这些事件。以UIButton为例,当用户单击某个UIB
    utton对象时,无论当前的第一响应对象是哪个视图,该对象都会收到指定的动作消息。
    当上第一响应对象吃力不讨好么。。。所以只能由某个UIResponder明确表示自己愿意成
    为第一响应对象才行。(我不知道设计上是基于什么考虑。。。安全?)

    在当上第一响应对象时,不同对象可能会有一些特殊的表现。例如UITextField当上的时
    候,就会调出一块小键盘。

    第一响应对象也有可能被辞退。发送一个resignFirstResponder,就可以劝退。

    4. 第一响应对象的任务

    刚才说了第一响应对象可以处理摇动。就来看个范例吧:

     代码如下:

    - (void) motionBegan:(UIEventSubtype)motion withEvent:(UIEvent *)event
    {
        if(motion == UIEventSubtypeMotionShake)
        {
            NSLog(@"Device is beginning to shake");
            [selfsetCircleColor:[UIColorredColor]];
            [selfsetNeedsDisplay];
        }
    }

    当摇动开始时触发某些行为。

    5. 获取当前第一响应对象

    源自这篇讨论:http://stackoverflow.com/questions/1823317/get-the-current-firs
    t-responder-without-using-a-private-api

    提问的家伙用了如下的方式来获取

    代码如下:

    UIView   *firstResponder = [keyWindow 
    performSelector:@selector(firstResponder)];
    结果被苹果打回来,说用了非公开的API。。。

    于是这家伙只好苦逼地用递归了:

    代码如下:

    implementationUIView (FindFirstResponder)
    - (UIView *)findFirstResponder
    {
        if (self.isFirstResponder) {        
            return self;     
        }
        for (UIView *subView in self.subviews) {
            UIView *firstResponder = [subView findFirstResponder];
            if (firstResponder != nil) {
                return firstResponder;
            }
        }
        return nil;
    } 
    @end

    6.View的FirstResponder的释放问题

    今天遇到一个问题,当我隐藏掉一个正在接受用户输入的UITextField的时候,键盘并不会消失,而且键盘仍然接受用户输入,再次显示该TextField时候发现在隐藏状态下,所有的输入仍然传输到了该TextField中,于是查下官方资料找到如下解释:
     
    Important If you hide a view that is currently the first responder, the view does not automatically resign its first responder status. Events targeted at the first responder are still delivered to the hidden view. To prevent this from happening, you should force your view to resign the first responder status when you hide it.    
     
      意思是如果这个View是当前的第一响应者的时候,隐藏该View并不会自动放弃其第一响应者的身份,而且会继续以第一响应者的身份接受消息。我们可以通过在隐藏View之前,手动调用resignFirstResponder来强制该view放弃第一响应者身份。
     
      下面请看小例子:

    .h代码如下:

    SvTestFirstResponder.h
     
    
    //
    //  SvTestFirstResponder.h
    //
    //  Created by maple on 3/15/12.
    //  Copyright (c) 2012 SmileEvday. All rights reserved.
    //
    //  当一个view时当前响应者时,调用其hidden方法并不会自动放弃第一响应者身份,所有的消息仍然会发送到这个view
    //  可以通过在hidden前强制放弃第一响应者,恢复正常的消息传递
    //
    
    #import <UIKit/UIKit.h>
    
     
    
    @interface SvTestFirstResponder : UIView {
        UITextField *_inputField;
    }
    
    @end
    .m代码如下:

    SvTestFirstResponder.m
     
    
    //
    //  SvTestFirstResponder.m
    //
    //  Created by maple on 3/15/12.
    //  Copyright (c) 2012 SmileEvday. All rights reserved.
    //
    
    #import "SvTestFirstResponder.h"
    
    @interface SvTestFirstResponder()
    
    - (void)hiddenInputView:(id)sender;
    - (void)showInputView:(id)sender;
    
    @end
    
    @implementation SvTestFirstResponder
    
    - (id)initWithFrame:(CGRect)frame
    {
        self = [super initWithFrame:frame];
        if (self) {
            // Initialization code
            
            _inputField = [[UITextField alloc] initWithFrame:CGRectMake(0, 0, 200, 50)];
            _inputField.center = CGPointMake(160, 50);
            [_inputField setFont:[UIFont systemFontOfSize:24]];
            _inputField.text = @"input you text";
            _inputField.clearsOnBeginEditing = YES;
            _inputField.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter; 
            _inputField.borderStyle = UITextBorderStyleRoundedRect;
            [self addSubview:_inputField];
            _inputField.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin;
            
            UIButton *hiddenBtn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
            hiddenBtn.frame = CGRectMake(0, 0, 115, 40);
            hiddenBtn.center = CGPointMake(80, 110);
            [hiddenBtn setTitle:@"Hide TextField" forState:UIControlStateNormal];
            [hiddenBtn addTarget:self action:@selector(hiddenInputView:) forControlEvents:UIControlEventTouchUpInside];
            [self addSubview:hiddenBtn];
            hiddenBtn.autoresizingMask = UIViewAutoresizingFlexibleRightMargin;
            hiddenBtn.titleLabel.lineBreakMode = UILineBreakModeTailTruncation;
            
            UIButton *showBtn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
            showBtn.frame = CGRectMake(0, 0, 115, 40);
            showBtn.center = CGPointMake(240, 110);
            [showBtn setTitle:@"Show TextField" forState:UIControlStateNormal];
            [showBtn addTarget:self action:@selector(showInputView:) forControlEvents:UIControlEventTouchUpInside];
            [self addSubview:showBtn];
            showBtn.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin;
            showBtn.titleLabel.lineBreakMode = UILineBreakModeTailTruncation;
        }
        return self;
    }
    
    /*
    // Only override drawRect: if you perform custom drawing.
    // An empty implementation adversely affects performance during animation.
    - (void)drawRect:(CGRect)rect
    {
        // Drawing code
    }
    */
    
    - (void)hiddenInputView:(id)sender
    {
        _inputField.hidden = YES;
    }
    
    - (void)showInputView:(id)sender
    {
        _inputField.hidden = NO;
    }
    
    @end

    这个简单的例子中,当输入框进入接受用户输入状态的时候,点击hide按钮,键盘并不会消失而且会继续接收用户输入并且将用户输入传到TextField中去,后面再点击Show按钮的时候你会发现所有在隐藏状态下输入的文字都已经成功的被接收。我们可以修改hide方法如下:

    代码如下:

    - (void)hiddenInputView:(id)sender
     
    
    {
    
        if (_inputField.isFirstResponder) {
    
            [_inputField resignFirstResponder];
    
        }
    
        _inputField.hidden = YES;
    
    }

      这样就可以在隐藏之前强制释放第一响应者身份,这个问题比较细节,但有时候可能就是这种细节问题导致一些莫名奇妙的问题,在隐藏一些可能成为第一响应者的view之前添加强制释放第一响应者身份,可能会帮我们避免一些奇怪的问题,而且也几乎不会有什么开销,何乐而不为呢。

    原文链接:解析iOS开发中的FirstResponder第一响应对象

    自己写的一个小例子,给UITableView添加扩展,监听cell的键盘响应事件,对应修改偏移量

    .h代码如下

    #import <UIKit/UIKit.h>
    
    @interface UITableView (Keyboard)
    /**
     *  添加tableview的cell的键盘监听事件,修改对应cell的偏移量
     */
    - (void)addResponserKeyboardNotification;
    @end

    .m代码如下

    #import "UITableView+Keyboard.h"
    /**
     *  保存当前的偏移y值,便于在键盘隐藏时恢复修改前的偏移量
     */
    static CGFloat currY = 0.f;
    
    @implementation UITableView (Keyboard)
    - (void)addResponserKeyboardNotification{
        currY = 0.f;
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHide:) name:UIKeyboardWillHideNotification object:nil];
    }
    
    - (void)keyboardWillShow:(NSNotification*)noti{
        //    NSLog(@"----%@
    %@",noti.userInfo,noti.object);
        
        for (UITableViewCell *infoCell in self.visibleCells) {
            if ([infoCell isKindOfClass:[UITableViewCell class]]) {
                
                UIView *resView = [self findFirstResponder:infoCell];
                
                
                CGRect tvToView = [resView convertRect:resView.frame toView:self];
                CGRect keyboardFrame = [noti.userInfo[UIKeyboardFrameEndUserInfoKey] CGRectValue];
    //            NSLog(@"
    --tv:%@
    --kb:%@",NSStringFromCGRect(tvToView),NSStringFromCGRect(keyboardFrame));
                if (CGRectGetMaxY(tvToView) + 64 >= keyboardFrame.origin.y) {
                    currY = self.contentOffset.y;
                    CGFloat nowY = currY;
                    nowY += CGRectGetMaxY(tvToView) + 64 - keyboardFrame.origin.y;
                    self.contentOffset = CGPointMake(0, nowY);
                }
                //                [self showMask];
                
            }
            
        }
        
        
        
    }
    /**
     *  递归找到当前键盘的响应者
     */
    - (UIView *)findFirstResponder:(UIView *)superView
    {
        if (superView.isFirstResponder) {
            return superView;
        }
        for (UIView *subView in superView.subviews) {
            UIView *firstResponder = [self findFirstResponder:subView];
            if (firstResponder != nil) {
                return firstResponder;
            }
        }
        return nil;
    }
    /**
     *  恢复键盘出现前的偏移y值
     */
    - (void)keyboardWillHide:(NSNotification*)noti{
        self.contentOffset = CGPointMake(0, currY);
    }
    @end
  • 相关阅读:
    Tensorflow从入门到精通之——Tensorflow基本操作
    Tensorflow从入门到精通之——Tensorflow基本操作
    卷积神经网络概述-七月在线机器学习集训营手把手教你从入门到精通卷积神经网络
    梳理百年深度学习发展史-七月在线机器学习集训营助你把握深度学习浪潮
    K-means聚类 的 Python 实现
    我在 B 站学习深度学习(生动形象,跃然纸上)
    我在 B 站学机器学习(Machine Learning)- 吴恩达(Andrew Ng)【中英双语】
    Eclipse新建Java工程出现红色感叹号怎么解决?
    SQL Server 2012安装时报错,错误 0x80070422怎么解决?解决方法。
    Shift键的三个妙用!Word又现神操作!
  • 原文地址:https://www.cnblogs.com/On1Key/p/5306747.html
Copyright © 2011-2022 走看看