• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

ios - 每次都没有调用drawRect

[复制链接]
菜鸟教程小白 发表于 2022-12-13 11:49:59 | 显示全部楼层 |阅读模式 打印 上一主题 下一主题

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

  1. touchbegan 上 - 通过调用 [keysLayer setNeedsDisplay];
  2. 绘制突出显示
  3. On touchEnd - 再次调用 [keysLayer setNeedsDisplay];
  4. 移除突出显示

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

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

如果有人可以帮助我。

更新


添加代码

在已添加 KeysLayer View 的 SuperView 中。

-(void)touchesBeganNSSet *)touches withEventUIEvent *)event {
    [super touchesBegan:touches withEvent:event];

    keysLayer.drawHighlight=YES;
    keysLayer.touchLocation=[touch locationInView:self];
    [keysLayer setNeedsDisplay];
}

-(void)touchesEndedNSSet *)touches withEventUIEvent *)event {
    [super touchesEnded:touches withEvent:event];

    keysLayer.drawHighlight=NO;
    [keysLayer setNeedsDisplay];
}

KeysLayer UIView 类

- (void)setTouchLocationCGPoint)location{
    self.touchLocation=location;

    bezierPathForHighlight=[self createHighLightPath];//Creating simple path based on touch location.
}

- (void)drawRectCGRect)rect {
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextSaveGState(context);

    if(!self.drawHighlight)
       [bezierPathForHighlight removeAllPoints];

    [bezierPathForHighlight stroke];
    CGContextRestoreGState(context);

}

更新 2


所以,我发现了这个问题,它只发生在我的 iPhone6iOS9.0 中。我用这个 test application 验证了这一点.令人惊讶的是,在这个测试应用程序中,我发现有一个特定的触摸区域,iOS 不会将 drawRect 称为 iOS BUG?

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

enter image description here

谢谢。



Best Answer-推荐答案


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

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

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

这是您需要添加的一些示例代码。

- (void)viewDidAppearBOOL)animated {
    [super viewDidAppear:animated];

    for (UIGestureRecognizer *gesture in self.view.window.gestureRecognizers) {
        gesture.delaysTouchesBegan = NO;
    }
}

UIViewController 的 View 出现后添加此代码。

关于ios - 每次都没有调用drawRect,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33015212/

回复

使用道具 举报

懒得打字嘛,点击右侧快捷回复 【右侧内容,后台自定义】
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

关注0

粉丝2

帖子830918

发布主题
阅读排行 更多
广告位

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap