当前位置: 首页 > 工具软件 > 3D Tableview > 使用案例 >

iOS 3D Touch开发tableview页面内的使用

庄子平
2023-12-01

上一次写了3DTouch在app icon的使用,时间隔了那么久,其实我早就写完了····就是一直忙着没时间

发表文章分享,也是对不住了~

这篇文章讲的是3DTouch在页面内的使用 

首先声明  childVC(3DTOUCH 的页面)

@interface ViewController ()<UITableViewDataSource,UITableViewDelegate,UIViewControllerPreviewingDelegate>
{
    UITableView * table;
    NSArray * array;
    _DPreViewController * childVC;
}
@end

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationController.navigationBar.translucent = NO;
    // 初始化数据源
    array = [NSArray arrayWithObjects:@"大部分地区晴天",@"西部地区为阴天",@"东部地区有小雨",@"南部地区晴转多云", nil];
    
    self.view.backgroundColor = [UIColor blueColor];
    [self createTab];
    // Do any additional setup after loading the view, typically from a nib.
}

// 3DTouch的动作关联tableview的index
# pragma mark 3D-Touch
- (UIViewController *)previewingContext:(id)context viewControllerForLocation:(CGPoint) point
{
    // 获取tableview点击的位置
    NSIndexPath *indexPath = [table indexPathForCell:(UITableViewCell* )[context sourceView]];
    
    NSArray * arr = [NSArray arrayWithObjects:@"11.gif",@"22.gif",@"33.gif",@"44.gif", nil];
//    NSArray *colorArr = @[[UIColor redColor],[UIColor blackColor],[UIColor greenColor],[UIColor blackColor]];
    
    // 这是即将显示的3Dtouch 出现效果的页面
    childVC = [[_DPreViewController alloc]init];
    [childVC getBackgruandcolor:[UIColor whiteColor] getData:[array objectAtIndex:indexPath.row] imageName:[arr objectAtIndex:indexPath.row]];
    
    childVC.preferredContentSize = CGSizeMake(0.0f,600.f);
    return childVC;
}

// 千万不要忘记设置代理
- (void)previewingContext:(id <UIViewControllerPreviewing>)previewingContext commitViewController:(UIViewController *)viewControllerToCommit
{
    [self showViewController:viewControllerToCommit sender:self];
}
然后接下来是3Dtouch页面的设置:

// 初始化数据
- (void)getBackgruandcolor:(UIColor *)color getData:(NSString *)str imageName:(NSString * )name
{
    self.view.backgroundColor = color;
    datastr = str;
    imagename = name;
    [self createView];
}

// <span style="white-space:pre">	</span>3dtouch页面的元素 
- (void)createView
{
    UIImageView * image = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height / 2)];
    [image setImage:[UIImage imageNamed:imagename]];
    [self.view addSubview:image];
    
    UILabel * lable = [[UILabel alloc]initWithFrame:CGRectMake(0, self.view.frame.size.height / 2, self.view.frame.size.width, self.view.frame.size.height / 2)];
    lable.textAlignment = NSTextAlignmentCenter;
    lable.text = datastr;
    lable.font = [UIFont systemFontOfSize:20];
    [self.view addSubview:lable];
    
}

// 这是3Dtouch出现的页面 上滑以后出现的菜单选项每一个item都是一个选项
-(NSArray<id<UIPreviewActionItem>> *)previewActionItems
{
    UIPreviewAction * action1 = [UIPreviewAction actionWithTitle:@"这就是所谓的3DTouch" style:0 handler:^(UIPreviewAction * _Nonnull action, UIViewController * _Nonnull previewViewController) {
        
//        PViewController *aaaa = [[PViewController alloc] init];
//        [[self topViewController] presentViewController:aaaa animated:YES completion:nil];
    }];
    
    UIPreviewAction * action2 = [UIPreviewAction actionWithTitle:@"随便点,点不坏" style:0 handler:^(UIPreviewAction * _Nonnull action, UIViewController * _Nonnull previewViewController) {
        UIViewController *aaaa = [[UIViewController alloc] init];
        aaaa.view.backgroundColor = [UIColor brownColor];
        [[self topViewController].navigationController pushViewController:aaaa animated:YES];
        
    }];
    UIPreviewAction * action3 = [UIPreviewAction actionWithTitle:@"多去玩玩吧~挺好玩" style:0 handler:^(UIPreviewAction * _Nonnull action, UIViewController * _Nonnull previewViewController) {
        
    }];
    
    NSArray * actions = @[action1,action2,action3];
    
    return actions;
}
// 以下两个为默认设定,直接复制粘贴就行
// 根视图的返回
- (UIViewController*)topViewController
{
    return [self topViewControllerWithRootViewController:[AppDelegate appDelegate].window.rootViewController];
}
// 根视图的类型
- (UIViewController*)topViewControllerWithRootViewController:(UIViewController*)rootViewController
{
    if ([rootViewController isKindOfClass:[UITabBarController class]]) {
        UITabBarController *tabBarController = (UITabBarController *)rootViewController;
        return [self topViewControllerWithRootViewController:tabBarController.selectedViewController];
    } else if ([rootViewController isKindOfClass:[UINavigationController class]]) {
        UINavigationController* navigationController = (UINavigationController*)rootViewController;
        return [self topViewControllerWithRootViewController:navigationController.visibleViewController];
    } else if (rootViewController.presentedViewController) {
        UIViewController* presentedViewController = rootViewController.presentedViewController;
        return [self topViewControllerWithRootViewController:presentedViewController];
    } else {
        return rootViewController;
    }
}


 类似资料: