Unity适配iPhone X—关于Home键指示器适配

C#

浏览数:203

2019-8-30

Unity摊

相信有些人在iPhone X上玩游戏的时候,总是误触底部白条(Home键指示器,暂且先叫“底部白条”),专业术语:“App需要有从状态栏下拉或底部栏上滑的操作,而这个会和系统的下拉或上滑调出通知中心手势冲突。” 。 于是开始 百度一下

这是什么鬼

这并不是我们想要的解决办法……. how to 办?

经过一系列的尝试, 在iOS11之后iOS增加的新的API,是关于解决手势冲突的问题。

@interface UIViewController (UIScreenEdgesDeferringSystemGestures)

// Override to return a child view controller or nil. If non-nil, that view controller's screen edges deferring system gestures will be used. If nil, self is used. Whenever the return value changes, -setNeedsScreenEdgesDeferringSystemGesturesUpdate should be called.
- (nullable UIViewController *)childViewControllerForScreenEdgesDeferringSystemGestures API_AVAILABLE(ios(11.0)) API_UNAVAILABLE(watchos, tvos);

// Controls the application's preferred screen edges deferring system gestures when this view controller is shown. Default is UIRectEdgeNone.
- (UIRectEdge)preferredScreenEdgesDeferringSystemGestures API_AVAILABLE(ios(11.0)) API_UNAVAILABLE(watchos, tvos);

// This should be called whenever the return values for the view controller's screen edges deferring system gestures have changed.
- (void)setNeedsUpdateOfScreenEdgesDeferringSystemGestures API_AVAILABLE(ios(11.0)) API_UNAVAILABLE(watchos, tvos);

@end

API我们知道了,可是加到哪里?然后通过分析Unity打包后的Xcode工程,在工程目录

Classes->UI->UnityViewControllerBaseiOS.mm

的脚本中,发现被重写的方法,如下:

- (UIRectEdge)preferredScreenEdgesDeferringSystemGestures
{
    UIRectEdge res = UIRectEdgeNone;
if(UnityGetDeferSystemGesturesTopEdge())
    res |= UIRectEdgeTop;
if(UnityGetDeferSystemGesturesBottomEdge())
    res |= UIRectEdgeBottom;
if(UnityGetDeferSystemGesturesLeftEdge())
    res |= UIRectEdgeLeft;
if(UnityGetDeferSystemGesturesRightEdge())
    res |= UIRectEdgeRight;
return res;
}

我们只需要改成如下即可:

UIRectEdge res = UIRectEdgeNone;
//if(UnityGetDeferSystemGesturesTopEdge())
    //res |= UIRectEdgeTop;
//if(UnityGetDeferSystemGesturesBottomEdge())
    //res |= UIRectEdgeBottom;
//if(UnityGetDeferSystemGesturesLeftEdge())
    //res |= UIRectEdgeLeft;
//if(UnityGetDeferSystemGesturesRightEdge())
    //res |= UIRectEdgeRight;
    return UIRectEdgeAll;

设置后第一次下拉“底部白条”只会展示指示器,继续下拉才能将通知中心拉出来。如果返回UIRectEdgeNone则会直接下拉出来。

参考博客,给大家安利一下,写的非常棒!:

iPhoneX 适配实践

PS: 如有疑问可以留言,一起学习。

作者:Jens丶