菜鸟教程小白 发表于 2022-12-12 09:06:16

ios - 在主视图中处理 touchesBegan - 如何获取被触摸的 subview ?


                                            <p><p>在单 ViewiPhone 应用程序中,我具有以下结构:<code>scrollView -> contentView -> imageView</code> 和下方(在主视图中)有 7 个可拖动的自定义 View- 可以从/到 <code>contenView</code>。</p>

<p>最初拖动是在自定义 View 中处理的(参见 <a href="https://github.com/afarber/ios-newbie/blob/2eb672523a9dcc313e6cf8f940bf42884fed6645/ScrollContent/ScrollContent/Tile.m" rel="noreferrer noopener nofollow">Tile.m@2eb672523a</a>),虽然效果很好,但需要向主视图发送通知。</p>

<p>所以我试图将 <code>touchesBegan</code> 和 friend 移动到主应用程序 View (参见 <a href="https://github.com/afarber/ios-newbie/blob/master/ScrollContent/ScrollContent/ViewController.m" rel="noreferrer noopener nofollow">ViewController.m</a>)。</p>

<p><strong>我的问题是:</strong></p>

<p>最初,我在 <code>contentView</code> 中有这段代码(参见 <a href="https://github.com/afarber/ios-newbie/blob/master/ScrollContent/ScrollContent/GameBoard.m" rel="noreferrer noopener nofollow">GameBoard.m</a> ),以便在 <code>scrollView</code> 上启用 subview 拖动:</p>

<pre><code>- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event
{
    UIView* result = ;

    return result == self ? nil : result;
}
</code></pre>

<p>但是,当尝试将图 block 从 <code>contentView</code> 拖回主视图时(按照以下屏幕截图所示的方向),这不会将 <code>touchesBegan</code> 传递到主视图查看。</p>

<p> <img src="/image/UCijQ.png" alt="app screenshot"/> </p>

<p>所以我改变了这个方法来返回主视图而不是图 block :</p>

<pre><code>- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event
{
    UIView* result = ;

    return result == self ? nil : self.window.rootViewController.view;
}
</code></pre>

<p>现在 <code>touchesBegan</code> 在主视图上被调用(正如我在调试器中看到的那样),但 <code>touches.view</code> 也指向主视图。</p>

<p><strong>我的问题是:</strong> </p>

<p>如何在新的<code>touchesBegan</code>方法中找到被触摸的tile?</p></p>
                                    <br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
                                            <p><p>没关系,我已经解决了我的问题。</p>

<p>在 <a href="https://github.com/afarber/ios-newbie/blob/master/ScrollContent/ScrollContent/ViewController.m" rel="noreferrer noopener nofollow">ViewController.m</a>我添加了一个 <code>_draggedTile</code> ivar 并在 <code>touchesBegan</code> 中设置了它,方法是使用以下方法查找触摸的瓷砖:</p>

<pre><code>- (SmallTile*) findTileAtPoint:(CGPoint)point withEvent:(UIEvent*)event
{
    NSArray* children = ;

    for (UIView* child in children) {
      CGPoint localPoint = ;

      if (] &amp;&amp;
            ) {
            return (SmallTile*)child;
      }
    }

    return nil;
}
</code></pre>

<p>而在 <code>touchedMoved</code>、<code>touchesEnded</code>、<code>touchesCancelled</code> 中我只使用了那个 ivar(最初我使用的是上面的 <code>findTileAtPoint:withEvent: </code> 方法也在那里,但有时触摸不会完全在拖动的图 block 上,并且移动不平稳或被取消)。</p></p>
                                   
                                                <p style="font-size: 20px;">关于ios - 在主视图中处理 touchesBegan - 如何获取被触摸的 subview ?,我们在Stack Overflow上找到一个类似的问题:
                                                        <a href="https://stackoverflow.com/questions/22781755/" rel="noreferrer noopener nofollow" style="color: red;">
                                                                https://stackoverflow.com/questions/22781755/
                                                        </a>
                                                </p>
                                       
页: [1]
查看完整版本: ios - 在主视图中处理 touchesBegan - 如何获取被触摸的 subview ?