1.桌面快捷菜單項
效果如圖:
桌面快捷菜單
點選之後的效果如圖:
點選桌面快捷菜單的效果
接下來看下具體實作:
1).在-application:didFinishLaunchingWithOptions:
方法中用-setShortcutItems:方法來添加快捷菜單項。
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
//3D Touch按壓程式圖示的快捷項
//快捷菜單的圖示
UIApplicationShortcutIcon *icon1=[UIApplicationShortcutIcon iconWithType:UIApplicationShortcutIconTypeCaptureVideo];
UIApplicationShortcutIcon *icon2=[UIApplicationShortcutIcon iconWithType:UIApplicationShortcutIconTypeAdd];
UIApplicationShortcutIcon *icon3=[UIApplicationShortcutIcon iconWithTemplateImageName:@"search"];
//快捷菜單
UIApplicationShortcutItem *item1=[[UIApplicationShortcutItem alloc]initWithType:@"1"
localizedTitle:@"嘿嘿"
localizedSubtitle:nil
icon:icon1
userInfo:nil];
UIApplicationShortcutItem *item2=[[UIApplicationShortcutItem alloc]initWithType:@"1"
localizedTitle:@"呵呵"
localizedSubtitle:@"幹嘛去洗澡"
icon:icon2
userInfo:nil];
UIApplicationShortcutItem *item3=[[UIApplicationShortcutItem alloc]initWithType:@"1"
localizedTitle:@"搜尋"
localizedSubtitle:nil
icon:icon3
userInfo:nil];
//設定app的快捷菜單
[[UIApplication sharedApplication] setShortcutItems:@[item1,item2,item3]];
//導航
self.window.rootViewController=[[UINavigationController alloc]initWithRootViewController:[ViewController new]];
return YES;
}
2).在-application:performActionForShortcutItem:completionHandler:方法中實作點選快捷菜單的方法:
//3D Touch按壓程式圖示的快捷項時觸發的方法
-(void)application:(UIApplication )application performActionForShortcutItem:(UIApplicationShortcutItem )shortcutItem completionHandler:(void (^)(BOOL))completionHandler
{
NSString title;
if([shortcutItem.localizedTitle isEqualToString:@"嘿嘿"])
{
title=@"嘿嘿";
}
else if([shortcutItem.localizedTitle isEqualToString:@"呵呵"])
{
title=@"呵呵";
}
else if([shortcutItem.localizedTitle isEqualToString:@"搜尋"])
{
title=@"搜尋";
}
//這裡就彈個框子意思一下
//由于UIAlertView在iOS 9被廢棄,是以選用UIAlertController
UIAlertController alertController=[UIAlertController alertControllerWithTitle:@"提示"
message:[NSString stringWithFormat:@"你點選了“%@”",title]
preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction action=[UIAlertAction actionWithTitle:@"知道了"
style:UIAlertActionStyleDefault
handler:^(UIAlertAction action) {
[alertController dismissViewControllerAnimated:YES completion:nil];
}];
[alertController addAction:action];
[self.window.rootViewController presentViewController:alertController
animated:YES
completion:nil];
}
2.程式内的3DTouch按壓操作:
表視圖
3DTouch按壓的時候生成的預覽
peek時上拉出現的菜單
1).首先,實作3DTouch的視圖控制器要遵守:
UIViewControllerPreviewingDelegate協定,它有2個required級别的協定方法:
-previewingContext:viewControllerForLocation:
以及
-previewingContext:commitViewController:
2).檢測3DTouch是否可用,并注冊3DTouch:
檢測3D Touch是否可用
-(BOOL)is3DTouchAvailiable
{
if(self.traitCollection.forceTouchCapability==UIForceTouchCapabilityAvailable)
return YES;
return NO;
}
注冊3DTouch
if([self is3DTouchAvailiable])
{
[self registerForPreviewingWithDelegate:self sourceView:self.view];
}
3).實作協定方法:
-(UIViewController *)previewingContext:(id<UIViewControllerPreviewing>)previewingContext viewControllerForLocation:(CGPoint)location
{
NSIndexPath *indexPath=[_tbVew indexPathForRowAtPoint:CGPointMake(location.x, location.y-64)];
if(indexPath)
{
DetailViewController *detail=[[DetailViewController alloc]init];
detail.title=_dataArray[indexPath.row];
//detail.preferredContentSize=CGSizeMake(300, 300);
__weak typeof(self) wkSelf=self;
//------------上拉時的菜單-------------------
//置頂及其點選邏輯
UIPreviewAction *topAction=[UIPreviewAction actionWithTitle:@"置頂" style:UIPreviewActionStyleDefault handler:^(UIPreviewAction * action, UIViewController * previewViewController) {
[wkSelf.dataArray exchangeObjectAtIndex:indexPath.row withObjectAtIndex:0];
[wkSelf.tbVew reloadData];
[wkSelf showAlert:@"提示" body:@"已置頂"];
}];
//删除及其點選邏輯
UIPreviewAction *deleteAction=[UIPreviewAction actionWithTitle:@"删除" style:UIPreviewActionStyleDestructive handler:^(UIPreviewAction *action, UIViewController * previewViewController) {
[wkSelf.dataArray removeObjectAtIndex:indexPath.row];
[wkSelf.tbVew reloadData];
[wkSelf showAlert:@"警告" body:@"已删除"];
}];
//傳遞上拉菜單項給detail
detail.actions=@[topAction,deleteAction];
return detail;
}
return nil;
}
-(void)previewingContext:(id<UIViewControllerPreviewing>)previewingContext commitViewController:(UIViewController *)viewControllerToCommit
{
[self showViewController:viewControllerToCommit sender:self];
}
4).要previewing的視圖控制器必須實作:
-(NSArray<id<UIPreviewActionItem>> *)previewActionItems方法,該方法定義了peek時上拉出來的菜單:
/**peek時上拉出來的菜單*/
-(NSArray<id<UIPreviewActionItem>> *)previewActionItems
{
return self.actions;
}