drawRect 不是每次都调用
drawRect not called everytime
我遇到了一个奇怪的问题,我正在做一个键盘扩展,我需要在触摸时绘制高亮键。我成功地做到了这一点,但是奇怪的是,对于左上角的一些键,特别是键 Q 和 A,不要绘制突出显示每次。主要是我的代码看起来像这样..
- 在
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 class
- (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 :)。所以在我看来,那些地方有 delay in touches
。
所以我开始调试不同地方的触摸,发现 _UISystemGestureGateGestureRecognizer
这是一个私人 class 在这些区域有不同的 属性 我所做的就是循环附加到 UIWindow
的手势识别器并将 delaysTouchesBegan
属性 设置为 NO
就是这样。
问题已解决。实际上这个问题并不特定于那个特定区域,而是屏幕的左上角可能 Apple
已经为它做了一些实现。虽然它现在工作正常。我会继续我的观察,但暂时它是一个适合你的工作修复。
这是您需要添加的一些示例代码。
- (void)viewDidAppear:(BOOL)animated {
[super viewDidAppear:animated];
for (UIGestureRecognizer *gesture in self.view.window.gestureRecognizers) {
gesture.delaysTouchesBegan = NO;
}
}
在视图出现后将此代码添加到您的 UIViewController
中。
我遇到了一个奇怪的问题,我正在做一个键盘扩展,我需要在触摸时绘制高亮键。我成功地做到了这一点,但是奇怪的是,对于左上角的一些键,特别是键 Q 和 A,不要绘制突出显示每次。主要是我的代码看起来像这样..
- 在
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 class
- (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 :)。所以在我看来,那些地方有 delay in touches
。
所以我开始调试不同地方的触摸,发现 _UISystemGestureGateGestureRecognizer
这是一个私人 class 在这些区域有不同的 属性 我所做的就是循环附加到 UIWindow
的手势识别器并将 delaysTouchesBegan
属性 设置为 NO
就是这样。
问题已解决。实际上这个问题并不特定于那个特定区域,而是屏幕的左上角可能 Apple
已经为它做了一些实现。虽然它现在工作正常。我会继续我的观察,但暂时它是一个适合你的工作修复。
这是您需要添加的一些示例代码。
- (void)viewDidAppear:(BOOL)animated {
[super viewDidAppear:animated];
for (UIGestureRecognizer *gesture in self.view.window.gestureRecognizers) {
gesture.delaysTouchesBegan = NO;
}
}
在视图出现后将此代码添加到您的 UIViewController
中。