拾取和弹出力触摸手势有时不起作用

Pick & pop force touch gesture not working sometimes

我有一个视图控制器,它在 viewDidLoad 方法中注册预览:

- (void) viewDidLoad
{
    [super viewDidLoad];

    if ([self.traitCollection respondsToSelector:@selector(forceTouchCapability)] &&
        (self.traitCollection.forceTouchCapability == UIForceTouchCapabilityAvailable))
    {
        [self registerForPreviewingWithDelegate:self sourceView:self.view];
    }
}

但有时无法识别强制触摸,就好像视图控制器没有注册预览一样。

问题是从状态恢复启动应用程序。 修复:将预览注册代码放在 viewWillAppear:

- (void) viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];

    if ([self.traitCollection respondsToSelector:@selector(forceTouchCapability)] &&
        (self.traitCollection.forceTouchCapability == UIForceTouchCapabilityAvailable))
    {
        [self registerForPreviewingWithDelegate:self sourceView:self.view];
    }
}

状态恢复调用viewDidLoad时,注册预览的时机正好