在UITableViewCell Objective C Code上进行3D Touch Peek和Pop.(强制触摸)

joy*_*dep 8 iphone objective-c ios 3dtouch force-touch

我需要使用Force Touch在Objective C中启用Peek And Pop功能UITableViewCell.并且还需要在默认邮件应用程序的窥视视图下显示一些操作.我是iOS新手,请帮助我到达那里.

Har*_*hna 10

TableViewCell上的Peek和Pop效果以及带动作的Collection View Cell

1)您应该将调用者viewController类作为UIViewControllerPreviewing Delegate来处理

@interface MyTableViewController()

2)创建一个@property来存储信息.

@property (nonatomic, strong) id previewingContext;
Run Code Online (Sandbox Code Playgroud)

3)在ViewDidLoad中调用forceTouchIntialize方法

- (void)viewDidLoad {
    [super viewDidLoad];
    [self forceTochIntialize];
}
Run Code Online (Sandbox Code Playgroud)

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;
}
Run Code Online (Sandbox Code Playgroud)

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;

}
Run Code Online (Sandbox Code Playgroud)

6)深压机中的POP(FOR POP)

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

7)当控制器看到并向上滑动时,如果要添加删除等按钮,则将此方法添加到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];
}
Run Code Online (Sandbox Code Playgroud)