3D touch手势,peek手势,pop手势,ios开发

/**

 *  当前结果页面

 */

.h文件中加入协议

@interface SVCurrentResultViewCtrl: SVViewController <UIViewControllerPreviewingDelegate>

.m文件中写

// 判断3D Touch是否可用,可用则注册

- (void)viewDidLoad

{

    [super viewDidLoad];

    if (self.traitCollection.forceTouchCapability == UIForceTouchCapabilityAvailable)

    {

        [self registerForPreviewingWithDelegate:self sourceView:uiview];

    }

 }

/**

 *  peek手势

 */

- (UIViewController *)previewingContext:(id<UIViewControllerPreviewing>)previewingContext

              viewControllerForLocation:(CGPoint)location

{

    // 获取用户手势点所在cell的下标。同时判断手势点是否超出tableView响应范围。

    if (![self getShouldShowRectAndIndexPathWithLocation:location])

    {

        return nil;

    }

    // 弹出视图的初始位置,sourceRect是peek触发时的高亮区域。这个区域内的View会高亮显示,其余的会模糊掉

    previewingContext.sourceRect = sourceRect;

    // 获取数据进行传值

    UIButton *currentBtn = _buttons[selectedPath.section];

    SVDetailViewCtrl *childVC = [[SVDetailViewCtrl alloc] init];

    [childVC setTestId:_resultModel.testId];

    [childVC setTestType:[NSString stringWithFormat:@"%ld", (long)currentBtn.tag]];

    return childVC;

}

/**

 *  pop手势

 */

- (void)previewingContext:(id<UIViewControllerPreviewing>)previewingContext

     commitViewController:(UIViewController *)viewControllerToCommit

{

    UIButton *currentBtn = _buttons[selectedPath.section];

    [self CellDetailClick:currentBtn

                 testType:[NSString stringWithFormat:@"%ld", (long)currentBtn.tag]];

}

/**

 *  获取用户手势点所在cell的下标,同时判断手势点是否超出tableview的范围

 */

- (BOOL)getShouldShowRectAndIndexPathWithLocation:(CGPoint)location

{

    // 坐标点的转化,

    CGPoint tableLocation = [self.view convertPoint:location toView:_tableView];

    selectedPath = [_tableView indexPathForRowAtPoint:tableLocation];

    // 如果selctedPath是nil,则说明越界

    if (!selectedPath)

    {

        return NO;

    }

    // 计算弹出视图的初始位置

    sourceRect = CGRectMake (0, NavBarH + StatusBarH + selectedPath.section * FITHEIGHT (239),

                             kScreenW, FITHEIGHT (209));

    // 如果row越界了,返回NO 不处理peek手势

    return (selectedPath.section >= (_buttons.count)) ? NO : YES;

}

@end

/**

 *  详情结果页面

 */

 .h中

#import <UIKit/UIKit.h>

@interface SVDetailViewCtrl : SVViewController <UIPreviewActionItem>

.m中

/**

 *  3D Touch 上移显示的视图

 */

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

{

    // 如果testType不为空,说明是从当前结果页面跳转过来的,所以不需要显示删除

    if (testType && ![testType isEqualToString:@""])

    {

        return nil;

    }

    // 实现删除当前结果功能

    UIPreviewAction *delAction = [UIPreviewAction

    actionWithTitle:I18N (@"Delete")

              style:UIPreviewActionStyleDefault

            handler:^(UIPreviewAction *_Nonnull action, UIViewController *_Nonnull previewViewController) {

              SVInfo (@"删除当前结果,testId=%lld", testId);

              //从数据库中删除

              [_db

              executeUpdate:[NSString

                            stringWithFormat:@"delete from SVSummaryResultModel where testId=%lld;", testId]];

              //从数据库中删除

              [_db

              executeUpdate:[NSString

                            stringWithFormat:@"delete from SVDetailResultModel where testId=%lld;", testId]];

              // 创建一个消息对象

              NSNotification *notice =

              [NSNotification notificationWithName:@"reloadResult" object:nil userInfo:nil];

              //发送消息

              [[NSNotificationCenter defaultCenter] postNotification:notice];

            }];

    //想要显示多个就定义多个 UIPreviewAction

    NSArray *actions = @[delAction];

    return actions;

}

@end

原文地址:https://www.cnblogs.com/wangbinios/p/5509993.html