菜鸟教程小白 发表于 2022-12-13 11:49:59

ios - 每次都没有调用drawRect


                                            <p><p>我陷入了一个奇怪的问题,我正在做一个键盘扩展,我需要在触摸时绘制键的高光。我成功地做到了这一点,但奇怪的是,对于左上角的某些键,特别是键 <strong>Q</strong> 和 <strong>A</strong>,不要每次都突出显示。主要是我的代码看起来像这样..</p>

<ol>
<li>在 <code>touchbegan</code> 上 - 通过调用 <code>;</code></li> 绘制突出显示
<li>On <code>touchEnd</code> - 再次调用 <code>;</code></li> 移除突出显示
</ol>

<p>所以基本上,<strong><code>drawRect</code></strong> 函数不会每次都在这些特定键上被调用,其他一切正常,甚至 <code>setNeedsDisplay</code> 被调用.</p>

<p>所以,我正在寻求帮助,<strong>drawRect</strong> 函数调用失败的原因,我想知道原因列表。</p>

<p>如果有人可以帮助我。</p>

<p><strong>更新</strong> </p>

<hr/>

<p>添加代码</p>

<p>在已添加 KeysLayerView 的 SuperView 中。</p>

<pre><code>-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    ;

    keysLayer.drawHighlight=YES;
    keysLayer.touchLocation=;
    ;
}

-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
    ;

    keysLayer.drawHighlight=NO;
    ;
}
</code></pre>

<p><strong>KeysLayer UIView 类</strong></p>

<pre><code>- (void)setTouchLocation:(CGPoint)location{
    self.touchLocation=location;

    bezierPathForHighlight=;//Creating simple path based on touch location.
}

- (void)drawRect:(CGRect)rect {
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextSaveGState(context);

    if(!self.drawHighlight)
       ;

    ;
    CGContextRestoreGState(context);

}
</code></pre>

<p><strong>更新 2</strong></p>

<hr/>

<p>所以,我发现了这个问题,它只发生在我的 <strong>iPhone6</strong> 和 <strong>iOS9.0</strong> 中。我用这个 <a href="https://www.dropbox.com/s/z1nzh5w0ptnprqs/Touch%20Test.zip?dl=0" rel="noreferrer noopener nofollow"><strong>test application</strong></a> 验证了这一点.令人惊讶的是,在这个测试应用程序中,我发现有一个特定的触摸区域,iOS 不会将 <strong>drawRect</strong> 称为 <strong>iOS BUG?</strong></p>

<p>请参阅下面的附图,它解释了实际发生的位置。我们有问题,没有解决办法。需要帮助。</p>

<p> <a href="/image/8wWf5.png" rel="noreferrer noopener nofollow"><img src="/image/8wWf5.png" alt="enter image description here"/></a> </p>

<p>谢谢。</p></p>
                                    <br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
                                            <p><p>这个问题似乎是特定于 <code>iOS 9</code> 以后的版本。我也可以在我的其他设备中复制它。问题似乎与 <code>UIWindow</code> 手势识别器有关。当我检查您的(@iphonic)示例代码时,我发现如果我在该特定位置点击并按住一秒钟,则会调用 drawRect :)。所以在我看来,那些地方有 <code>delay in touch</code>。 </p>

<p>所以我开始调试不同位置的触摸并发现 <code>_UISystemGestureGateGestureRecognizer</code> 这是一个私有(private)类在这些区域具有不同的属性,而我所做的只是循环通过附加到 <code>UIWindow</code> 并将 <code>delaysTouchesBegan</code> 属性设置为 <code>NO</code> 就可以了。</p>

<p>问题已解决。实际上问题并不特定于该特定区域,而是屏幕的左角可能是 <code>Apple</code> 已经为它做了一些实现。虽然它现在工作正常。我将继续我的观察,但目前它对你来说是一个有效的修复。</p>

<p>这是您需要添加的一些示例代码。</p>

<pre><code>- (void)viewDidAppear:(BOOL)animated {
    ;

    for (UIGestureRecognizer *gesture in self.view.window.gestureRecognizers) {
      gesture.delaysTouchesBegan = NO;
    }
}
</code></pre>

<p>在 <code>UIViewController</code> 的 View 出现后添加此代码。</p></p>
                                   
                                                <p style="font-size: 20px;">关于ios - 每次都没有调用drawRect,我们在Stack Overflow上找到一个类似的问题:
                                                        <a href="https://stackoverflow.com/questions/33015212/" rel="noreferrer noopener nofollow" style="color: red;">
                                                                https://stackoverflow.com/questions/33015212/
                                                        </a>
                                                </p>
                                       
页: [1]
查看完整版本: ios - 每次都没有调用drawRect