標籤:
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