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

时间:2017-06-13 05:55:50

标签: ios objective-c iphone 3dtouch force-touch

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

1 个答案:

答案 0 :(得分:10)

使用操作对TableViewCell和集合查看单元格进行偷看和弹出效果

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

@interface MyTableViewController()

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

@property (nonatomic, strong) id previewingContext;

3)在ViewDidLoad中调用forceTouchIntialize方法

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

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

7)当控制器看到并向上滑动时,如果要添加删除等按钮,则将此方法添加到previewViewContrller中(在此示例中为“PushViewController&#34;”

- (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];
}