Simulate "not available Force Touch" vibrations and fades

63 Views Asked by At

I am trying to simulate the same effect that has pressing a non-enabled force touch app icon on the home screen inside an app. It starts fading as normal, and once you press deeply, it returns to its normal state and makes three vibrations (and doesn't open the app).

I couldn't find any documentation on Apple regarding this explicit case, so there's my effort simulating so far:

func previewingContext(previewingContext: UIViewControllerPreviewing, viewControllerForLocation location: CGPoint) -> UIViewController? {
    // I have a list of 'supported items' that will return the correct preview ViewController, 
    // and some of them are not supported returning the following lines:

    // IMPORTANT TODO: Verify this is "Legal"
    AudioServicesPlaySystemSound(1521)

    return nil
}

Problems so far: The 'non pressed' views don't fade. After the vibrations, if you don't move the touch when you release it the view acts as it was 'tapped' (this is a problem because I am detecting a tap and opening another view)

1

There are 1 best solutions below

0
On

The solution was to return a ViewController that would close himself on viewDidAppear:. Here's an example:

// In UIViewControllerPreviewingDelegate
func previewingContext(previewingContext: UIViewControllerPreviewing, viewControllerForLocation location: CGPoint) -> UIViewController? {
      return PeekPopInvisibleViewController()
}

// Custom class to simulate 'no option':
private class PeekPopInvisibleViewController: UIViewController {

   init() {
      super.init(nibName: nil, bundle: nil)
      view.backgroundColor = UIColor.clearColor()
   }

   required init?(coder aDecoder: NSCoder) {
      super.init(coder: aDecoder)
   }

   override func viewDidAppear(animated: Bool) {
      dismissViewControllerAnimated(false, completion: nil)

      // This plays the '3 vibration' effect
      AudioServicesPlaySystemSound(1521)
   }
}