一、系统home界面弹出一个列表
#import "AppDelegate.h"
#import "ViewController.h"
@interface AppDelegate ()
@property (strong, nonatomic) UINavigationController *nav;
@end
@implementation AppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
[self createIconTouch];
ViewController *VC = [[ViewController alloc]init];
VC.view.backgroundColor = [UIColor whiteColor];
VC.title = @"首页";
UINavigationController *nav = [[UINavigationController alloc]initWithRootViewController:VC];
[self.window setRootViewController:nav];
_nav = nav;
return YES;
}
- (void)createIconTouch
{
/** 初始化图标*/
UIApplicationShortcutIcon *icon1 = [UIApplicationShortcutIcon iconWithTemplateImageName:@"图片名称一"];
UIApplicationShortcutIcon *icon2 = [UIApplicationShortcutIcon iconWithTemplateImageName:@"图片名称二"];
/** 初始化home界面弹出的item*/
UIMutableApplicationShortcutItem *item = [[UIMutableApplicationShortcutItem alloc]initWithType:@"类型一" localizedTitle:@"标题一" localizedSubtitle:@"副标题一" icon:icon1 userInfo:nil];
UIMutableApplicationShortcutItem *item1 = [[UIMutableApplicationShortcutItem alloc]initWithType:@"类型二" localizedTitle:@"标题二" localizedSubtitle:@"副标题二" icon:icon2 userInfo:nil];
NSArray *items = @[item,item1];
[UIApplication sharedApplication].shortcutItems = items;
}
- (void)application:(UIApplication *)application performActionForShortcutItem:(UIApplicationShortcutItem *)shortcutItem completionHandler:(void (^)(BOOL))completionHandler {
// react to shortcut item selections
NSLog(@"点击了 %@, 类型:%@", shortcutItem.localizedTitle, shortcutItem.type);
UIViewController *VC = [[UIViewController alloc]init];
VC.view.backgroundColor = [UIColor whiteColor];
VC.title = [NSString stringWithFormat:@"点击了 %@, 类型:%@", shortcutItem.localizedTitle, shortcutItem.type];
[self.nav pushViewController:VC animated:YES];
}
二、在界面内部使用3DTouch
#import "ViewController.h"
#import "PreViewController.h"
@interface ViewController ()<UITableViewDataSource, UITableViewDelegate, UIViewControllerPreviewingDelegate>
@property (strong, nonatomic) UITableView *tableView;
@property (strong, nonatomic) UILongPressGestureRecognizer *longPress;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
UILongPressGestureRecognizer *longPressGr = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPressToDo)];
_longPress = longPressGr;
[self createTableView];
}
#pragma mark- 初始化TableView
- (void)createTableView
{
_tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height) style:UITableViewStylePlain];
_tableView.delegate = self;
_tableView.dataSource = self;
[self.view addSubview:_tableView];
}
- (void)longPressToDo{}
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
[self check3DTouch];
}
#pragma mark- 检测页面是否支持3DTouch
- (void)check3DTouch
{
if (self.traitCollection.forceTouchCapability == UIForceTouchCapabilityAvailable)
{
[self registerForPreviewingWithDelegate:self sourceView:self.tableView];
NSLog(@"3D Touch 开启");
//长按停止
_longPress.enabled = NO;
}
else
{
_longPress.enabled = YES;
}
}
#pragma mark- UIViewControllerPreviewingDelegate
- (UIViewController *)previewingContext:(id<UIViewControllerPreviewing>)previewingContext viewControllerForLocation:(CGPoint)location
{
NSIndexPath *indexPath = [_tableView indexPathForRowAtPoint:location];
UITableViewCell *cell = [self.tableView cellForRowAtIndexPath:indexPath];
/** 自定义一个控制器*/
PreViewController *vc = [[PreViewController alloc]init];
UILabel *label = [[UILabel alloc]initWithFrame:vc.view.frame];
label.backgroundColor = [UIColor cyanColor];
label.text = cell.textLabel.text;
vc.view = label;
return vc;
}
- (void)previewingContext:(id <UIViewControllerPreviewing>)previewingContext commitViewController:(UIViewController *)viewControllerToCommit
{
[self showViewController:viewControllerToCommit sender:self];
}
#pragma mark- UITableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return 30;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString *cellID = @"cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID];
if (cell == nil)
{
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellID];
}
cell.textLabel.text = [NSString stringWithFormat:@"第%ld行测试",indexPath.row];
return cell;
}
@end
#import "PreViewController.h"
@interface PreViewController ()
@end
@implementation PreViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (NSArray<id<UIPreviewActionItem>> *)previewActionItems{
UIPreviewAction *p1 =[UIPreviewAction actionWithTitle:@"分享" style:UIPreviewActionStyleDefault handler:^(UIPreviewAction * _Nonnull action, UIViewController * _Nonnull previewViewController) {
NSLog(@"点击了分享");
}];
UIPreviewAction *p2 =[UIPreviewAction actionWithTitle:@"收藏" style:UIPreviewActionStyleDestructive handler:^(UIPreviewAction * _Nonnull action, UIViewController * _Nonnull previewViewController) {
NSLog(@"点击了收藏");
}];
NSArray *actions = @[p1,p2];
return actions;
}
@end