UITableViewCell上的3D Touch Peek和Pop Objective C代码(Force Touch)

8

我需要在Objective C中使用Force Touch在UITableViewCell上启用Peek And Pop功能。并且还需要在预览视图下方显示一些操作,就像默认邮件应用程序一样。我对iOS不熟悉,请帮助我完成这个任务。


欢迎来到 Stack Overflow。您已经参观过[tour]并阅读了[ask]吗? - Picard
1个回答

11

在 TableViewCell 和 CollectionViewCell 上实现 Peek And Pop 效果与行为

1) 你应该将调用者的 ViewController 类指定为 UIViewControllerPreviewingDelegate。

@interface MyTableViewController ()

2) 创建一个属性来存储信息。

@property (nonatomic, strong) id previewingContext;

3) 在ViewDidLoad中调用forceTouchIntialize方法。

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

4)检查是否支持Force Touch

-(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) 指定我们想要预览的视图控制器(用于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)当控制器达到峰值,您向上滑动时,如果想添加像删除、存档这样的按钮,请将此方法添加到您的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];
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接