码迷,mamicode.com
首页 > 移动开发 > 详细

iOS9 3DTouch(2)——pop/peek

时间:2015-11-25 23:21:48      阅读:529      评论:0      收藏:0      [点我收藏+]

标签:

2.在App中,选中某个内容,可以使用peek(轻按)进行预览或者使用pop(重按)切换控制器,ians完整内容

peek:轻按,预览。3种状态:

peek可用,按中格浮现出来,其他的变模糊。

peek,可以快速预览接下来的controller内容

Peek+PreviewAction,快捷操作

pop:一般导航到peek状态下预览的控制器。

 

(1)如果forcetouch可用,则注册

-(void)viewDidLoad{

    [super viewDidLoad];

       //注册forceTouch

    if(self.traitCollection.forceTouchCapability == UIForceTouchCapabilityAvailable){

    [self registerForPreviewingWithDelegate:self sourceView:self.view];

    }

}

(2)设置代理并实现代理方法UIViewControllerPreviewingDelegate

  • -(UIViewController *)previewingContext:(id<UIViewControllerPreviewing>)previewingContext viewControllerForLocation:(CGPoint)location {
  •     //获取点击的cell
  •     NSIndexPath *indexPath = [self.tableView indexPathForRowAtPoint:location];
  •     UITableViewCell *cell = [self.tableView cellForRowAtIndexPath:indexPath];
  •     if (!cell) {
  •         return nil;
  •     }
  •    
  •     //pre-peek状态下,重点显示的区域
  •     previewingContext.sourceRect = cell.frame;
  •    
  •     HCViewController *vc = [[HCViewController alloc] init];
  •     vc.labelText = self.dataList[indexPath.row];
  •     //peek状态下,显示的view大小
  •     vc.preferredContentSize = CGSizeMake(0, 0);
  •    
  •     return vc;
  • }
  •  
  • - (void)previewingContext:(id <UIViewControllerPreviewing>)previewingContext commitViewController:(UIViewController *)viewControllerToCommit
  • {
  •     [self.navigationController pushViewController:viewControllerToCommit animated:YES];
  • }

(3)Peek中PreviewAction的实现

新增数组,储存PreviewAction

@property (nonatomic,strong) NSArray *arrayPreviewActions;

previewActionItems方法中返回存储PreviewAction的数组;

  • -(NSArray<id<UIPreviewActionItem>> *)previewActionItems
  • {
  •     return self.arrayPreviewActions;
  • }

设置PreviewAction数组内容,并实现功能。(即快捷操作)

  • -(void)viewDidLoad{
  •     [super viewDidLoad];
  •    
  •     self.view.backgroundColor = [UIColor whiteColor];
  •    
  •     [self label];
  •    
  •     //设置previewActions
  •     self.arrayPreviewActions = @[
  •                             [UIPreviewAction actionWithTitle:@"OK" style:UIPreviewActionStyleDefault handler:^(UIPreviewAction * _Nonnull action, UIViewController * _Nonnull previewViewController) {
  •                                 NSLog(@"Press OK");
  •                             }],
  •                             [UIPreviewAction actionWithTitle:@"Cancel" style:UIPreviewActionStyleDestructive handler:^(UIPreviewAction * _Nonnull action, UIViewController * _Nonnull previewViewController) {
  •                                 NSLog(@"Press Cancel");
  •                             }],
  •                             ];
  • }

技术分享技术分享

 

iOS9 3DTouch(2)——pop/peek

标签:

原文地址:http://www.cnblogs.com/destinationluo/p/4996191.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!