我陷入了一个奇怪的问题,我正在制作一个键盘扩展,其中我需要在触摸时突出显示按键。我成功地做到了这一点,但奇怪的是,对于左上角的某些键,特别是 Q 和 A 键,并不是每次都突出显示。主要是我的代码看起来像这样..
- On
touchbegan
- 通过调用[keysLayer setNeedsDisplay]绘制高亮显示;
- On
touchEnd
- 通过再次调用[keysLayer setNeedsDisplay]; 删除突出显示
所以基本上, drawRect
函数不会每次在这些特定键上被调用,其他一切都工作正常,甚至 setNeedsDisplay
被调用.
所以,我正在寻求帮助,什么可能会导致 drawRect 函数调用失败,我想知道原因列表。
如果有人可以帮助我。
更新
添加代码
在已添加 KeysLayer View 的 SuperView 中。
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
[super touchesBegan:touches withEvent:event];
keysLayer.drawHighlight=YES;
keysLayer.touchLocation=[touch locationInView:self];
[keysLayer setNeedsDisplay];
}
-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
[super touchesEnded:touches withEvent:event];
keysLayer.drawHighlight=NO;
[keysLayer setNeedsDisplay];
}
KeysLayer UIView 类内部
- (void)setTouchLocation:(CGPoint)location{
self.touchLocation=location;
bezierPathForHighlight=[self createHighLightPath];//Creating simple path based on touch location.
}
- (void)drawRect:(CGRect)rect {
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSaveGState(context);
if(!self.drawHighlight)
[bezierPathForHighlight removeAllPoints];
[bezierPathForHighlight stroke];
CGContextRestoreGState(context);
}
更新2
所以,我发现了这个问题,它只发生在我的iPhone6和iOS9.0上。我用这个test application验证了这一点。令人惊讶的是,在这个测试应用程序中,我发现有一个特定的触摸区域,iOS 不会调用 drawRect ,这是一个 iOS BUG?
请参阅下面的附图,其中解释了它实际发生的位置。我们有问题,却没有解决办法。需要帮助。
谢谢。
最佳答案
该问题似乎特定于 iOS 9
及以上版本。我也可以在我的其他设备上复制它。问题似乎出在 UIWindow
手势识别器上。当我检查您的(@iphonic)示例代码时,我发现如果我在该特定位置点击并按住一秒钟,则将调用drawRect:)。所以在我看来,这些地方触摸有延迟
。
因此,我开始调试不同位置的触摸,发现 _UISystemGestureGateGestureRecognizer
这是一个私有(private)类,在这些区域具有不同的属性,我所做的就是循环遍历附加到 UIWindow
并将 delaysTouchesBegan
属性设置为 NO
就这样了。
问题已解决。实际上,问题并不特定于该特定区域,而是与屏幕的左角有关,可能是苹果已经为此做了一些实现。尽管它现在运行良好。我将继续我的观察,但目前它对您来说是一个可行的解决方案。
这是您需要添加的一些示例代码。
- (void)viewDidAppear:(BOOL)animated {
[super viewDidAppear:animated];
for (UIGestureRecognizer *gesture in self.view.window.gestureRecognizers) {
gesture.delaysTouchesBegan = NO;
}
}
在 View 出现后,在 UIViewController
中添加此代码。
关于ios - 不是每次都调用drawRect,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33015212/