How to implement Peek and Pop & show previewActionItems- iOS9 Feature (Part 2)

“Peek and Pop” is one of the great feature of 3D Touch. In my previous post I mentioned about “Home Screen Quick Actions” and adopting force touch, you can check this article here.
Peek And Pop feature allows users to preview a content (peek) with force touch and the content is opened in full size (popwith one more gentle move which keeps the previous touch up.

IMG_0040

In a very simple look, you should check force touch on device is available, or not. You should address your caller viewController class as UIViewControllerPreviewingDelegate. You should decide which view may register/listen peek & pop gesture in your caller-controller. Then the next step to implement the following delegate methods for peek and pop respectively.

– (UIViewController *)previewingContext:(id<UIViewControllerPreviewing>)previewingContext viewControllerForLocation:(CGPoint)location;

– (void)previewingContext:(id )previewingContext commitViewController: (UIViewController*)viewControllerToCommit;

In Detail;

To listen peek gesture, register the view which you decide to grasp the touch for preview: (viewDidLoad is a nice method to write these lines.)

//May the Force Touch Be With You

if ([self isForceTouchAvailable]) {
   self.previewingContext = [self registerForPreviewingWithDelegate:self sourceView:self.view];
}

To be a good citizien, you should check your users force touch settings are on or changed via this following two methods, respectively:

- (BOOL)isForceTouchAvailable {
  BOOL isForceTouchAvailable = NO;
  if ([self.traitCollection respondsToSelector:@selector(forceTouchCapability)]) {
      isForceTouchAvailable = self.traitCollection.forceTouchCapability == UIForceTouchCapabilityAvailable;
  }
  return isForceTouchAvailable;
}
- (void)traitCollectionDidChange:(UITraitCollection *)previousTraitCollection {
   [super traitCollectionDidChange:previousTraitCollection];
   if ([self isForceTouchAvailable]) {
      if (!self.previewingContext) {
         self.previewingContext = [self registerForPreviewingWithDelegate:self sourceView:self.view];
     }
   } else {
       if (self.previewingContext) {
          [self unregisterForPreviewingWithContext:self.previewingContext];
self.previewingContext = nil;
       }
   }
}

If A ViewController will detect force touch and present B ViewController. A is the UIViewControllerPreviewingDelegate and implement the following methods I mentioned before.

- (UIViewController *)previewingContext:(id<UIViewControllerPreviewing>)previewingContext viewControllerForLocation:(CGPoint)location{

   CGPoint cellPosition = [self.tableView convertPoint:location fromView:self.view];
   NSIndexPath *path = [self.tableView indexPathForRowAtPoint:cellPosition];

   if (path) {
      UITableViewCell *tableCell = [self.tableView cellForRowAtIndexPath:path];
      QLPreviewController *previewController = [self prepareYourPreviewStatement];
      previewController.preferredContentSize = CGSizeMake(400, 400);
      previewingContext.sourceRect = [self.view convertRect:tableCell.frame fromView:(UIView*)self.tableView];
      return previewController;
   }
   return nil;
}
-(void)previewingContext:(id )previewingContext commitViewController: (UIViewController *)viewControllerToCommit {

   [self.navigationController showViewController:viewControllerToCommit sender:nil];

}

In Peek Action, you can show some action items below the preview content. In the Figure, you can see Copy, Share, Favourite, Delete PreviewActionItems. Implementation is very simple, you should implement -(NSArray <id> * ) previewActionItems method in your previewViewController. Please pay attention, not in the caller ViewController!! You can see the sample implementation in the following:

- (NSArray<id> *)previewActionItems {
__weak typeof(self) weakSelf = self
UIPreviewAction *previewAction1 = [UIPreviewAction actionWithTitle:@"This is action2" style:UIPreviewActionStyleDefault handler:^(UIPreviewAction *action,  UIViewController *previewViewController){
   [weakSelf openAccountStatement:nil];
}];

UIPreviewAction *previewAction2 = [UIPreviewAction actionWithTitle:@"This is action1" style:UIPreviewActionStyleDefault handler:^(UIPreviewAction *action, UIViewController *previewViewController){
   [weakSelf sendReceipt];
}];
return @[previewAction1,previewAction2];
}

PS: If you find this article useful please use like button 🙂 and if you have a question or a comment, don’t hesitate to share it!

May the Force Touch be with you! 🙂 

 

Advertisements

5 thoughts on “How to implement Peek and Pop & show previewActionItems- iOS9 Feature (Part 2)

  1. i don’t want peek and pop for my application. if i select uitextfield or alert view my app is crashing. i’m not implementing any of 3D touch things. Please help me why it’s crashing

  2. How can we implement the Peek feature as in the Phone App in iOS. Like, after opening the app and on force touching the Dial button, it will show action items , but it doesn’t disappear on releasing finger.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s