I need to enable Peek And Pop
functionality on a UITableViewCell
in Objective C by using Force Touch. And also need to show some actions under peek view like default mail app. I am new to iOS and please help me to get there.
Peek And Pop Effect on a TableViewCell and Collection View Cell With Actions
1)You should address your caller viewController class as UIViewControllerPreviewing Delegate
@interface MyTableViewController ()
2)Create a @property for storing the information.
@property (nonatomic, strong) id previewingContext;
3)Call the forceTouchIntialize method in ViewDidLoad
- (void)viewDidLoad {
[super viewDidLoad];
[self forceTochIntialize];
}
4)Check Force Touch Available or Not
-(void)forceTouchIntialize{
if ([self isForceTouchAvailable]) {
self.previewingContext = [self registerForPreviewingWithDelegate:self sourceView:self.view];
}
}
- (BOOL)isForceTouchAvailable {
BOOL isForceTouchAvailable = NO;
if ([self.traitCollection respondsToSelector:@selector(forceTouchCapability)]) {
isForceTouchAvailable = self.traitCollection.forceTouchCapability == UIForceTouchCapabilityAvailable;
}
return isForceTouchAvailable;
}
5)Specify the view controller which content we want to preview(FOR PEEK)
- (UIViewController *)previewingContext:(id<UIViewControllerPreviewing> )previewingContext viewControllerForLocation:(CGPoint)location{
CGPoint cellPostion = [yourTableView convertPoint:location fromView:self.view];
NSIndexPath *path = yourTableView indexPathForRowAtPoint:cellPostion];
if (path) {
UITableViewCell *tableCell = [yourTableView
cellForRowAtIndexPath:path];
//Pushing to a nib File
PushViewController *previewController = [[PushViewController alloc] initWithNibName:@"PushViewController" bundle:nil];
//To Pass data to Preview ViewController
// id temp = [yourDataArray objectAtIndex:path.row];
// PushViewController.anyObject=temp;
previewingContext.sourceRect = [self.view convertRect:tableCell.frame fromView: yourTableView
]; return previewController;
}
return nil;
}
6)POP in a Deep Press (FOR POP)
-(void)previewingContext:(id )previewingContext commitViewController: (UIViewController *)viewControllerToCommit {
[self.navigationController showViewController:viewControllerToCommit sender:nil];
}
7)When the controller get peek and you swipe up, If you want to add buttons like delete,archive add this method into your previewViewContrller(In this example “ PushViewController")
- (NSArray<id> *)previewActionItems {
UIPreviewAction *previewAction1 = [UIPreviewAction actionWithTitle:@"delete" style:UIPreviewActionStyleDefault handler:^(UIPreviewAction *action, UIViewController *previewViewController){
}];
UIPreviewAction *previewAction2 = [UIPreviewAction actionWithTitle:@"archive" style:UIPreviewActionStyleDefault handler:^(UIPreviewAction *action, UIViewController *previewViewController){
}];
return @[previewAction1,previewAction2];
}