iOS8.1- iOS8.3横屏后部分区域无响应bug 解决办法

xiaoxiao2021-02-28  81

注意通过[[UIApplication sharedApplication] setStatusBarOrientation:interfaceOrientation animated:YES];来设置屏幕旋转会导致

iOS version 8.1 ~ iOS version 8.3 之间的版本在横屏时出现home键后半部没有响应的情况。

对于点击事件会导致点击的坐标点不正确,但是对于滑动等手势不会造成影响,滑动拖动等手势一般都是根据相对位移来进行判断的。而点击事件大多都是根据绝对坐标进行判断的。

 如果因为使用[[UIApplication sharedApplication] setStatusBarOrientation:interfaceOrientation animated:YES]来旋转屏幕会出现iOS8.1-iOS8.3下出现home键后半部不响应点击事件的情况,而改用[[UIDevice currentDevice] setValue:rotation forKey:@"orientation"]来解决问题,就会出现顶部状态条不能跟随屏幕方向发送改变,另外对于键盘,声音大小的弹框等都不会更加设备的当前方向自动调整,就需要手动设置这些控件,特别是顶部状态条,在全屏下必须隐藏,这个可能会和设计的需求不一致。在iOS8.1-iOS8.3下解决不响应问题的方法为:

 重写hitTest方法将hitTest的坐标点转移到具有点击事件的视图上。

具体的内容可以看考demo:点击打开链接

关键部分为:

+ (BOOL)isNeedAdaptiveiOS8Rotation{ NSArray<NSString *> *versionStrArr = [[UIDevice currentDevice].systemVersion componentsSeparatedByString:@"."]; NSLog(@"versionArr is %@", versionStrArr); int firstVer = [[versionStrArr objectAtIndex:0] intValue]; int secondVer = [[versionStrArr objectAtIndex:1] intValue]; if (firstVer == 8) { if (secondVer >= 1 && secondVer <= 3) { return YES; } } return NO; } /** //对于iOS8.1 - iOS8.3版本使用 [[UIApplication sharedApplication] setStatusBarOrientation:interfaceOrientation animated:YES]; 导致home键后半部点击区域不响应的解决办法。 */ - (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event{ BOOL isNeedAdaptiveiOS8 = [[self class] isNeedAdaptiveiOS8Rotation]; UIInterfaceOrientation currentInterfaceOrientation = [UIApplication sharedApplication].statusBarOrientation; //对于iOS8.1 - iOS8.3版本 当点击按钮位于home键的下半区域时不响应点击事件,因此只需要处理不响应的情况即可 BOOL isNeedJudge = currentInterfaceOrientation == UIInterfaceOrientationLandscapeRight; if (isNeedAdaptiveiOS8 && isNeedJudge) { CGPoint btnPoint = [self convertPoint:point toView:self.clickBtn]; if ([self.clickBtn pointInside:btnPoint withEvent:event]) { //点击区域在clickBtn上 __weak typeof(self) weakSelf = self; dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.11 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{ [weakSelf clickBtnAction:self.clickBtn]; }); weakSelf.userInteractionEnabled = NO; dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.10 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{ weakSelf.userInteractionEnabled = YES; }); return self.clickBtn; } } return [super hitTest:point withEvent:event]; }

转载请注明原文地址: https://www.6miu.com/read-35075.html

最新回复(0)