iosobjective-cforce-touch

Pick & pop force touch gesture not working sometimes


I have a view controller which registers for previewing in viewDidLoad method:

- (void) viewDidLoad
{
    [super viewDidLoad];

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

but sometimes force touch is not recognized as if view controller didn't register for previewing.


Solution

  • The problem was starting the app from state restoration. Fix: place preview registering code in viewWillAppear:

    - (void) viewWillAppear:(BOOL)animated
    {
        [super viewWillAppear:animated];
    
        if ([self.traitCollection respondsToSelector:@selector(forceTouchCapability)] &&
            (self.traitCollection.forceTouchCapability == UIForceTouchCapabilityAvailable))
        {
            [self registerForPreviewingWithDelegate:self sourceView:self.view];
        }
    }
    

    When viewDidLoad is called from state restoration, the timing is right for registering for previewing.