何方神圣如此猖狂 發表於 2023-3-22 16:50:53

解密ios响应链的工作原理

<div id="navCategory"><h5 class="catalogue">目录</h5><ul class="first_class_ul"><li>事件响应链的工作原理</li><li>响应者对象的特点</li><li>自定义事件处理</li><li>事件传递和事件响应</li><li>事件拦截</li><li>事件传递到父视图</li><li>自定义事件响应链</li><li>总结</li></ul></div><p>事件响应链是 <code>iOS</code> 开发中的一个核心概念,它描述了系统将用户交互事件传递给最适合处理该事件的对象的过程。理解事件响应链的机制对于开发高质量的应用程序至关重要。本文将深入探讨事件响应链的工作原理,并提供 Swift 中的代码示例来帮助读者更好地理解这一概念。</p>
<p class="maodian"></p><h2>事件响应链的工作原理</h2>
<p>在 <code>iOS</code> 中,事件响应链的工作原理可以简单概括为:从最上层的 UIWindow 开始,依次向下传递事件,直到找到最适合处理事件的响应者对象为止。在这个过程中,每个响应者对象都有机会处理事件。</p>
<p>当用户执行一个操作时,如触摸屏幕或运动设备,系统会创建一个 UIEvent 对象,并将其发送到当前的第一响应者对象。如果第一响应者对象无法处理该事件,则系统会将该事件传递给响应者链中的下一个对象,直到找到能够处理该事件的对象。如果最终没有对象能够处理该事件,则事件被系统丢弃。</p>
<p>以下是事件响应链的示意图:</p>
<div class="jb51code"><pre class="brush:plain;">         UIWindow
             |
       UIViewController
             |
         UIView
             |
   subviews of UIView</pre></div>
<p>在这个示意图中,<code>UIWindow</code> 是响应者链的起点,它是所有视图的根视图。<code>UIViewController</code> 和 <code>UIView</code> 都是响应者对象,它们都可以处理事件。<code>UIViewController</code> 可以接收和处理来自其根视图的事件,而 <code>UIView</code> 可以接收和处理来自其自身的事件,以及来自其子视图的事件。</p>
<p class="maodian"></p><h2>响应者对象的特点</h2>
<p>响应者对象是一种特殊类型的对象,它们实现了 <code>UIResponder</code> 类。响应者对象可以处理事件,可以成为第一响应者对象,并且可以将事件传递给下一个响应者对象。以下是 <code>UIResponder</code> 类中的一些常用方法:</p>
<ul><li><code>canBecomeFirstResponder</code>:返回一个布尔值,指示对象是否可以成为第一响应者对象。</li><li><code>becomeFirstResponder</code>:将对象设置为第一响应者对象。</li><li><code>resignFirstResponder</code>:放弃第一响应者对象的地位。</li><li><code>next</code>:返回响应者链中的下一个响应者对象。</li></ul>
<p>响应者对象还可以实现许多方法来处理事件,例如:</p>
<ul><li><code>touchesBegan(_:with:)</code>:当用户在视图上开始触摸时调用。</li><li><code>touchesMoved(_:with:)</code>:当用户在视图上移动触摸时调用。</li><li><code>touchesEnded(_:with:)</code>:当用户在视图上结束触摸时调用。</li><li><code>touchesCancelled(_:with:)</code>:当系统取消触摸事件时调用。</li></ul>
<p class="maodian"></p><h2>自定义事件处理</h2>
<p>在 <code>Swift</code> 中,可以通过重写 <code>UIResponder</code> 子类的方法来自定义事件处理。以下是一个示例代码,展示如何重写 <code>UIView</code> 子类的 <code>touchesBegan</code> 方法来处理触摸事件:</p>
<div class="jb51code"><pre class="brush:cpp;">class CustomView: UIView {
    override func touchesBegan(_ touches: Set&lt;UITouch&gt;, with event: UIEvent?) {
      super.touchesBegan(touches, with: event)
      
      // 处理触摸事件
      // ...
    }
}</pre></div>
<p>在这个示例中,当用户在 <code>CustomView</code> 上开始触摸时,系统会调用 <code>CustomView</code> 的 <code>touchesBegan</code> 方法。在此方法中,开发者可以编写自己的触摸事件处理代码。</p>
<p class="maodian"></p><h2>事件传递和事件响应</h2>
<p>事件传递和事件响应是事件响应链的两个重要环节。在事件传递阶段,系统会将事件从上往下传递,直到找到最适合处理事件的对象。在事件响应阶段,系统会将事件从下往上响应,直到事件被处理或者传递到响应者链的顶部。</p>
<p>在事件传递阶段,<code>UIView</code> 和 <code>UIViewController</code> 都有一个 <code>hitTest( *:with:)</code> 方法,该方法返回一个 <code>UIView</code> 对象。当系统接收到事件时,它会调用 <code>hitTest(* :with:)</code> 方法来确定最适合处理该事件的视图对象。<code>hitTest( *:with:)</code> 方法首先检查自己是否能够处理该事件,如果不能,它会将事件传递给其子视图,并递归调用子视图的 <code>hitTest(* :with:)</code> 方法,直到找到能够处理该事件的视图对象。</p>
<p>在事件响应阶段,系统会将事件传递到第一响应者对象,并沿着响应者链向上传递,直到事件被处理或者传递到响应者链的顶部。在这个过程中,每个响应者对象都有机会处理事件。如果某个响应者对象能够处理事件,则它将调用相应的方法来处理事件,例如 <code>touchesBegan(_:with:)</code> 方法。如果该对象不能处理事件,则它将调用 <code>next</code> 方法,将事件传递给响应者链中的下一个对象。</p>
<p class="maodian"></p><h2>事件拦截</h2>
<p>在 <code>hitTest(_:with:)</code> 方法中,我们可以检查触摸点是否在指定区域内,如果在,则返回当前视图作为拦截目标,否则返回 <code>nil</code>,让系统将事件传递给下一个响应者。示例代码如下:</p>
<div class="jb51code"><pre class="brush:cpp;">class CustomView: UIView {
   
    override func hitTest(_ point: CGPoint, with event: UIEvent?) -&gt; UIView? {
      if self.bounds.contains(point) {
            // 触摸点在视图内,拦截事件
            return self
      } else {
            // 触摸点不在视图内,将事件传递给下一个响应者
            return super.hitTest(point, with: event)
      }
    }
   
    override func touchesBegan(_ touches: Set&lt;UITouch&gt;, with event: UIEvent?) {
      super.touchesBegan(touches, with: event)
      print("CustomView touchesBegan")
    }

}</pre></div>
<p>在上述代码中,我们重写了 <code>hitTest( *:with:)</code> 方法,并在该方法中检查触摸点是否在视图内。如果在,则返回当前视图作为拦截目标,否则返回 nil,让系统将事件传递给下一个响应者。在 <code>touchesBegan(* :with:)</code> 方法中,我们打印了一条日志,以便在触摸事件发生时能够看到该方法是否被调用。</p>
<p class="maodian"></p><h2>事件传递到父视图</h2>
<p>要将事件传递到父视图,可以调用 <code>next?.touchesBegan(touches, with: event)</code> 方法,让父视图处理触摸事件。示例代码如下:</p>
<div class="jb51code"><pre class="brush:cpp;">class CustomView: UIView {
    override func touchesBegan(_ touches: Set&lt;UITouch&gt;, with event: UIEvent?) {
      super.touchesBegan(touches, with: event)
      
      // 处理触摸事件
      // 如果无法处理该事件,传递给父视图进行处理
      next?.touchesBegan(touches, with: event)
    }
}

class ParentView: UIView {
    override func touchesBegan(_ touches: Set&lt;UITouch&gt;, with event: UIEvent?) {
      super.touchesBegan(touches, with: event)
      
      // 处理触摸事件
    }
}</pre></div>
<p>在上面的示例中,<code>CustomView</code> 和 <code>ParentView</code> 都是 <code>UIView</code> 的子类。当用户在 <code>CustomView</code> 上触摸时,<code>CustomView</code> 的 <code>touchesBegan</code> 方法会被调用。在这个方法中,如果 <code>CustomView</code> 无法处理该事件,它会将该事件传递给其父视图(<code>ParentView</code>)进行处理。</p>
<p>通过 <code>next?.touchesBegan(touches, with: event)</code> 方法将事件传递给父视图,如果父视图能够处理该事件,它的 <code>touchesBegan</code> 方法将被调用。在这个方法中,可以处理触摸事件。如果父视图仍然无法处理该事件,该事件将被传递给更高级别的响应对象进行处理。</p>
<p>需要注意的是,当事件被传递到下一个响应对象时,会调用该对象的 <code>touchesBegan</code> 方法。因此,在这个方法中,可以对事件进行处理,也可以将其传递给更高级别的响应对象进行处理。</p>
<p class="maodian"></p><h2>自定义事件响应链</h2>
<p>在 <code>iOS</code> 中,每个视图都是一个 <code>UIResponder</code> 对象。<code>UIResponder</code> 是一个抽象类,它定义了响应者对象可以处理的事件类型,包括触摸事件、加速计事件、远程控制事件等。每个 <code>UIResponder</code> 对象都有一个 <code>next</code> 响应者,即下一个响应者对象。当一个事件发生时,系统会将该事件从前往后依次传递给响应者链中的对象,直到某个对象处理了该事件为止。如果没有任何对象处理该事件,则该事件将被丢弃。</p>
<p>我们可以通过自定义 <code>UIResponder</code> 子类来实现更灵活的事件处理逻辑。下面是一个简单的自定义响应者链的示例代码:</p>
<div class="jb51code"><pre class="brush:cpp;">class CustomResponder: UIResponder {
   
    override func touchesBegan(_ touches: Set&lt;UITouch&gt;, with event: UIEvent?) {
      super.touchesBegan(touches, with: event)
      print("CustomResponder touchesBegan")
      next?.touchesBegan(touches, with: event)
    }
   
}

class ViewController: UIViewController {
   
    override func touchesBegan(_ touches: Set&lt;UITouch&gt;, with event: UIEvent?) {
      super.touchesBegan(touches, with: event)
      print("ViewController touchesBegan")
    }
   
}</pre></div>
<p>在上面的代码中,我们定义了一个名为 <code>CustomResponder</code> 的自定义响应者子类。在该类中,我们重写了 <code>touchesBegan(_:with:)</code> 方法,并在该方法中打印了一条日志。在该方法中,我们还调用了 <code>next?.touchesBegan(touches, with: event)</code> 方法,将触摸事件传递给下一个响应者对象。</p>
<p>在 ViewController 中,我们也重写了 <code>touchesBegan( *:with:)</code> 方法,并在该方法中打印了一条日志。当触摸事件发生时,首先会调用 <code>CustomResponder</code> 的 <code>touchesBegan(* :with:)</code> 方法,并打印出 &quot;<code>CustomResponder touchesBegan</code>&quot;。然后,由于我们调用了 <code>next?.touchesBegan(touches, with: event)</code> 方法,系统会将触摸事件传递给 <code>CustomResponder</code> 的下一个响应者对象,即 <code>ViewController</code>。此时,系统会调用 <code>ViewController</code> 的 <code>touchesBegan(_:with:)</code> 方法,并打印出 &quot;<code>ViewController touchesBegan</code>&quot;。</p>
<p>通过自定义响应者子类,我们可以更加灵活地处理事件,实现更复杂的事件处理逻辑。例如,我们可以在响应者链中添加多个响应者对象,根据事件类型、触摸点位置等条件来决定哪个响应者对象处理该事件。</p>
<p class="maodian"></p><h2><span style="font-size:18px">总结</span></h2>
<p>事件响应链是 <code>iOS</code> 开发中的一个核心概念。了解事件响应链的工作原理和实现方式,可以帮助开发者更好地理解 <code>iOS</code> 应用的交互模型,编写更高效、可靠的交互代码。</p>
<p>以下是一些事件响应链的实践建议:</p>
<ul><li>在处理触摸事件时,始终调用父类的 <code>touchesBegan( *:with:)</code>、<code>touchesMoved(* :with:)</code>、<code>touchesEnded( *:with:)</code> 和 <code>touchesCancelled(* :with:)</code> 方法。这样可以确保事件会正确地传递到响应者链的下一个对象。</li><li>如果希望在事件传递过程中拦截事件,可以重写 <code>hitTest(_:with:)</code> 方法,并在该方法中检查事件是否应该被拦截。</li><li>如果希望将事件传递到父视图,可以调用 <code>next?.touchesBegan(touches, with: event)</code> 方法。</li><li>尽可能避免使用 <code>touches</code> 属性,因为该属性在多点触控环境下会出现问题。推荐使用 <code>allTouches</code> 属性来获取所有触摸点。</li><li>尽可能避免使用 <code>gesture recognizer</code> 来处理触摸事件,因为这会增加系统的负担,并可能导致意外的行为。推荐使用触摸事件处理方法来处理触摸事件。</li></ul>
<p>&nbsp;以上就是解密ios响应链的工作原理的详细内容,希望本篇文章可以更好地帮助大家理解 iOS 应用的交互模型。更多精彩内容请大家继续关注琼殿技术社区,我们将进行更多关于ios内容的更新。</p>
                           
                            <div class="art_xg">
                              <b>您可能感兴趣的文章:</b><ul><li>iOS 中事件的响应链和传递链</li><li>iOS开发之事件传递响应链</li></ul>
                            </div>

                        </div>
                        <!--endmain-->
頁: [1]
查看完整版本: 解密ios响应链的工作原理