8

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

4

1 回答 1

11

使用操作对 TableViewCell 和集合视图单元格进行 Peek 和 Pop 效果

1)你应该将你的调用者 viewController 类作为 UIViewControllerPreviewing Delegate

@interface MyTableViewController ()

2)创建一个@property 用于存储信息。

@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)指定视图控制器我们要预览哪些内容(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)当控制器被窥视并且你向上滑动时,如果你想添加像删除这样的按钮,存档将此方法添加到你的 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];
}
于 2017-06-13T06:02:17.630 回答