iOS9 3DTouch(2)——pop/peek

來源:互聯網
上載者:User

標籤:

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

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.