3D Touch Peek and Pop by UITableViewCell Objective-C Code. (Force Touch)

I need to enable the Peek And Pop function in a UITableViewCell in lens c, using the power of touch.And I also need to show some actions under the guise of viewing, as the default email application. I am new to iOS and please help me get there.

+4
source share
1 answer

Peek and Pop effect for TableViewCell and collection with actions

1) You must refer to the viewController class of the calling object as a UIViewControllerPreviewing Delegate

@interface MyTableViewController ()

2) Create @property to store information.

@property (nonatomic, strong) id previewingContext;

3) forceTouchIntialize ViewDidLoad

- (void)viewDidLoad {
    [super viewDidLoad];
    [self forceTochIntialize];
}

4) " "

-(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) , (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 ( POP)

-(void)previewingContext:(id )previewingContext commitViewController: (UIViewController *)viewControllerToCommit {
    [self.navigationController showViewController:viewControllerToCommit sender:nil];
}

7) , , delete, previewViewContrller ( "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];
}
+5

Source: https://habr.com/ru/post/1679097/


All Articles