iOS 8存在一个bug,当iPad处于“倒置竖屏”(Home键在上方)或“左横屏”(Home键在左侧)模式下,任何从正好位于右边缘开始的触摸都无法正确测试到相应的视图。
解决方法是子类化UIWindow
以正确测试右侧。
@implementation FixedWindow
- (UIView*)hitTest:(CGPoint)point withEvent:(UIEvent*)event
{
UIView* hit = [super hitTest:point withEvent:event];
if (!hit && point.x >= CGRectGetMaxX(self.bounds))
hit = [super hitTest:CGPointMake(point.x - 0.5, point.y) withEvent:event];
return hit;
}
@end
通过 window
属性将窗口附加到您的应用程序代理。
@implementation AppDelegate
- (UIWindow*)window
{
if (!_window)
_window = [[IVWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
return _window;
}
@end
在竖屏和右横屏模式下,我已确认右边缘的触摸始终至少离边缘0.5像素而不是恰好在边缘上,因此这个修复方法应该按照类似的方式工作。
扩展窗口框架
请注意,firebug的修复方法也可以使用,即略微扩展窗口框架以包括右侧。但是:
- 如果您在application:willFinishLaunchingWithOptions:或application:didFinishLaunchingWithOptions:中执行此操作,则视图层次结构不会调整为新框架,并且右边缘的触摸将无法通过层次结构。
- 如果您旋转设备,则窗口可能无法正确居中。这会导致界面元素稍微模糊或颠簸。
iOS 7:
iOS 7存在类似的错误,即命中测试也失败,但具有非零结果和未旋转的几何形状。这个修复方法应该适用于iOS 7和8。
@implementation FixedWindow
- (UIView*)hitTest:(CGPoint)point withEvent:(UIEvent*)event
{
UIView* hit = [super hitTest:point withEvent:event];
if (!hit || hit == self)
{
CGRect bounds = self.bounds;
hit = [super hitTest:CGPointMake(MIN(MAX(point.x, CGRectGetMinX(bounds) + 0.5), CGRectGetMaxX(bounds) - 0.5),
MIN(MAX(point.y, CGRectGetMinY(bounds) + 0.5), CGRectGetMaxY(bounds) - 0.5))
withEvent:event];
}
return hit;
}
@end