iOS 3D Touch使用

3D Touch 的第二個功能:預覽和彈出視圖


1、創建一個viewcontroller作爲window的rootviewcontroller(xcode7 這個需要設置,否則會崩潰)

           

2、創建一個預覽視圖控制器peekViewController,展示peek的內容,同時實現

3、viewcontroller控制器簽署協議

UIViewControllerPreviewingDelegate(該協議是iOS9新特性)


4、在viewdidload裏面檢測是否支持3D Touch,設置代理


//必須進行是否支持3D Touch,3D Touch可以被關閉,還有一些ios設備不支持

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

        

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

    }


5、在viewcontroller裏實現協議方法

//peek

- (nullable UIViewController *)previewingContext:(id <UIViewControllerPreviewing>)previewingContext viewControllerForLocation:(CGPoint)location NS_AVAILABLE_IOS(9_0)

{

    PeekViewController *vc = [[PeekViewController alloc]init];

    //設置預覽視圖的大小

    vc.preferredContentSize = CGSizeMake(0, 330);

    //設置點擊位置出現的白色塊的大小

    previewingContext.sourceRect = CGRectMake(20, location.y - 10, [UIScreen mainScreen].bounds.size.width - 40, 50);

    return vc;

}



//pop 方法

- (void)previewingContext:(id <UIViewControllerPreviewing>)previewingContext commitViewController:(UIViewController *)viewControllerToCommit NS_AVAILABLE_IOS(9_0)

{

//可以進行視圖跳轉等操作    

[self.Navgationviewcontroller pushViewController:viewControllerToCommit animated:NO completion:nil];

}




6、在peekviewcontroller裏面實現

-(NSArray<id<UIPreviewActionItem>> *)previewActionItems方法


-(NSArray<id<UIPreviewActionItem>> *)previewActionItems

{

    NSMutableArray *actionItemArray = [NSMutableArray array];

    

    UIPreviewAction *action1 = [UIPreviewAction actionWithTitle:@"自定義1" style:UIPreviewActionStyleSelected handler:^(UIPreviewAction * _Nonnull action, UIViewController * _Nonnull previewViewController) {

        

        //實現自己的邏輯處理

        

    }];

    UIPreviewAction *action2 = [UIPreviewAction actionWithTitle:@"自定義2" style:UIPreviewActionStyleSelected handler:^(UIPreviewAction * _Nonnull action, UIViewController * _Nonnull previewViewController) {

        

    }];

    

    

    [actionItemArray  addObject:action1];

    [actionItemArray addObject:action2];

    return actionItemArray;

}







發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章