3D touch

#import "BSTableViewController.h"

#import "BSDetailViewController.h"



@interface BSTableViewController ()<</span>UIViewControllerPreviewingDelegate,BSDetailViewControllerDelegate>


@property (nonatomic,strong)NSMutableArray *items;

@property (nonatomic,weak)UITableViewCell *selectedCell;


@end


@implementation BSTableViewController


- (NSMutableArray *)items {

    if (!_items) {

        _items = [[NSMutableArray alloc]initWithObjects:@"one",@"two",@"three",@"four",@"five",@"six",@"seven",@"eight",nil];

    }

    return _items;

}



- (void)viewDidLoad {

    [super viewDidLoad];

    

//    self.tableView.backgroundColor = [UIColor greenColor];

    self.title = @"3DTouchDemo";

    self.tableView.rowHeight = 100;

    // 重要

   

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

}


#pragma mark - UIViewControllerPreviewingDelegate


- (UIViewController *)previewingContext:(id<<span style="font-variant-ligatures: no-common-ligatures; color: #703daa">UIViewControllerPreviewing>)previewingContext viewControllerForLocation:(CGPoint)location {

    

    NSIndexPath *indexPath = [self.tableView indexPathForCell:(UITableViewCell* )[previewingContext sourceView]];

//    NSLog(@"---%d",indexPath.row);

    

    

//    self.selectedCell = [self searchCellWithPoint:location];

//    previewingContext.sourceRect = self.selectedCell.frame;

    BSDetailViewController *detailVC = [[BSDetailViewController alloc] init];

    detailVC.delegate = self;

    detailVC.navTitle = self.selectedCell.textLabel.text;

    return detailVC;

}


- (void)previewingContext:(id<<span style="font-variant-ligatures: no-common-ligatures; color: #703daa">UIViewControllerPreviewing>)previewingContext commitViewController:(UIViewController *)viewControllerToCommit {

    NSLog(@"fads");

    [self tableView:self.tableView didSelectRowAtIndexPath:[self.tableViewindexPathForCell:self.selectedCell]];

}


// 根据一个点寻找对应cell并返回cell

- (UITableViewCell *)searchCellWithPoint:(CGPoint)point {

    UITableViewCell *cell = nil;

    for (UIView *view in self.tableView.subviews) {

        NSString *class = [NSString stringWithFormat:@"%@",view.class];

        if (![class isEqualToString:@"UITableViewWrapperView"]) continue;

        for (UIView *tempView in view.subviews) {

            if ([tempView isKindOfClass:[UITableViewCell class]] && CGRectContainsPoint(tempView.frame, point)) {

                cell = (UITableViewCell *)tempView;

                break;

            }

        }

        break;

    }

    return cell;

}


#pragma mark - BSDetailViewControllerDelegate

- (void)detailViewControllerDidSelectedBackItem:(BSDetailViewController*)detailVC {

    NSLog(@"back");

}


- (void)detailViewController:(BSDetailViewController *)detailVC DidSelectedDeleteItem:(NSString *)navTitle {

    [self.items removeObject:navTitle];

    [self.tableView reloadData];

}

#pragma mark - Table view data source


- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {

    return 1;

}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

    return self.items.count;

}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    static NSString *ID = @"cellid";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];

    if (!cell) {

        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:ID];

//        [self registerForPreviewingWithDelegate:self sourceView:cell];

    }

    cell.selectionStyle = UITableViewCellSelectionStyleNone;

    

    cell.textLabel.text = self.items[indexPath.row];

    cell.detailTextLabel.text = [NSString stringWithFormat:@"%zd",indexPath.row + 1];

    

    return cell;

}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath*)indexPath {

    

    

    BSDetailViewController *detailVC = [[BSDetailViewController alloc] init];

    detailVC.navTitle = self.items[indexPath.row];

    

    [self.navigationController pushViewController:detailVC animated:YES];

}


@end


// 版权属于原作者

// http://code4app.com (cn) http://code4app.net (en)

// 发布代码于最专业的源码分享网站: Code4App.com





//

//  BSDetailViewController.h

//  3DTouchDemo

//

//  Created by roki on 16/1/26.

//  Copyright © 2016 roki. All rights reserved.

//


#import

@class BSDetailViewController;


@protocol BSDetailViewControllerDelegate <<span style="font-variant-ligatures: no-common-ligatures; color: #703daa">NSObject>


@required

// 删除按钮点击

- (void)detailViewController:(BSDetailViewController *)detailVC DidSelectedDeleteItem:(NSString *)navTitle;

// 返回按钮点击

- (void)detailViewControllerDidSelectedBackItem:(BSDetailViewController*)detailVC;


@end


@interface BSDetailViewController : UIViewController


@property (nonatomic,copy)NSString *navTitle;


@property (nonatomic,weak)id<</span>BSDetailViewControllerDelegate> delegate;


@end

// 版权属于原作者

// http://code4app.com (cn) http://code4app.net (en)

 

// 发布代码于最专业的源码分享网站: Code4App.com






//

//  BSDetailViewController.m

//  3DTouchDemo

//

//  Created by roki on 16/1/26.

//  Copyright © 2016 roki. All rights reserved.

//


#import "BSDetailViewController.h"


@interface BSDetailViewController ()


@end


@implementation BSDetailViewController


- (void)viewDidLoad {

    [super viewDidLoad];

    self.view.backgroundColor = [UIColor whiteColor];

    self.title = self.navTitle;

    UIImageView *imageView = [[UIImageView allocinitWithFrame:CGRectMake(86,100200150)];

    imageView.image = [UIImage imageNamed:@"imageone"];

    UILabel *githubName = [[UILabel allocinit];

    githubName.text = @"ITBigSea";

    githubName.textColor = [UIColor blueColor];

    githubName.backgroundColor = [UIColor colorWithRed:0.9 green:0.9 blue:0.9alpha:1];

    githubName.frame = CGRectMake(13627010030);

    githubName.textAlignment = NSTextAlignmentCenter;

    

    [self.view addSubview:imageView];

    [self.view addSubview:githubName];

}


- (void)didReceiveMemoryWarning {

    [super didReceiveMemoryWarning];

    // Dispose of any resources that can be recreated.

}


- (NSArray<<span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2">id<<span style="font-variant-ligatures: no-common-ligatures; color: #703daa">UIPreviewActionItem>> *)previewActionItems {

    //

    UIPreviewAction *action1 = [UIPreviewAction actionWithTitle:@"删除"style:UIPreviewActionStyleDefault handler:^(UIPreviewAction * _Nonnull action, UIViewController * _Nonnull previewViewController) {

        if ([self.delegaterespondsToSelector:@selector(detailViewController:DidSelectedDeleteItem:)]) {

            [self.delegate detailViewController:selfDidSelectedDeleteItem:self.navTitle];

        }

    }];

    //

    UIPreviewAction *action2 = [UIPreviewAction actionWithTitle:@"返回"style:UIPreviewActionStyleDefault handler:^(UIPreviewAction * _Nonnull action, UIViewController * _Nonnull previewViewController) {

        if ([self.delegate respondsToSelector:@selector(detailViewControllerDidSelectedBackItem:)]) {

            [self.delegate detailViewControllerDidSelectedBackItem:self];

        }

    }];

    

    NSArray *actions = @[action1,action2];

    

    return actions;

}

@end



如果添加图标上的3d touch

则在appdelegate中添加如下代码


- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {

    // Override point for customization after application launch.

    self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];

    

    UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:[[BSTableViewController alloc] init]];

    

    self.window.rootViewController = nav;

    

    // 动态添加快捷启动

    UIApplicationShortcutIcon *icon = [UIApplicationShortcutIcon iconWithType:UIApplicationShortcutIconTypeAlarm];

    UIApplicationShortcutItem *item = [[UIApplicationShortcutItem alloc] initWithType:@"shortcutTypeTwo" localizedTitle:@"two" localizedSubtitle:nil icon:icon userInfo:nil];

    [[UIApplication sharedApplication] setShortcutItems:@[item]];

    

    

    [self.window makeKeyAndVisible];

    return YES;

}


这是新增添的方法

- (void)application:(UIApplication *)application performActionForShortcutItem:(UIApplicationShortcutItem *)shortcutItem completionHandler:(void (^)(BOOL))completionHandler {

    

    UINavigationController *nav = (UINavigationController *)self.window.rootViewController;

    BSDetailViewController *detailVC = [[BSDetailViewController alloc] init];

    

    if ([shortcutItem.type isEqualToString:@"shortcutTypeOne"]) {

        detailVC.navTitle = @"one";

        

    } else if ([shortcutItem.type isEqualToString:@"shortcutTypeTwo"]) {

        detailVC.navTitle = @"two";

    }

    

    [nav pushViewController:detailVC animated:YES];

}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值