1. Desktop shortcut menu items
Effect
Desktop shortcut menu
Post-click Effects
Click the effect of the desktop shortcut menu
Next look at the specific implementation:
1). In-application:didfinishlaunchingwithoptions:
method, use the-setshortcutitems: method to add a shortcut menu item.
-(BOOL) Application: (UIApplication *) application didfinishlaunchingwithoptions: (Nsdictionary *) launchOptions {
Shortcuts for 3D Touch Press program icons
Icons for shortcut menus
Uiapplicationshortcuticon *icon1=[uiapplicationshortcuticon Iconwithtype: Uiapplicationshortcuticontypecapturevideo];
Uiapplicationshortcuticon *icon2=[uiapplicationshortcuticon Iconwithtype:uiapplicationshortcuticontypeadd];
Uiapplicationshortcuticon *icon3=[uiapplicationshortcuticon iconwithtemplateimagename:@ "Search"];
shortcut menu
Uiapplicationshortcutitem *item1=[[uiapplicationshortcutitem alloc]initwithtype:@ "1"
localizedtitle:@ "hehe"
Localizedsubtitle:nil
Icon:icon1
Userinfo:nil];
Uiapplicationshortcutitem *item2=[[uiapplicationshortcutitem alloc]initwithtype:@ "1"
localizedtitle:@ "hehe"
localizedsubtitle:@, "Why do you take a shower?"
Icon:icon2
Userinfo:nil];
Uiapplicationshortcutitem *item3=[[uiapplicationshortcutitem alloc]initwithtype:@ "1"
localizedtitle:@ "Search"
Localizedsubtitle:nil
Icon:icon3
Userinfo:nil];
Set the app's shortcut menu
[[UIApplication sharedapplication] setshortcutitems:@[item1,item2,item3];
Navigation
Self.window.rootviewcontroller=[[uinavigationcontroller Alloc]initwithrootviewcontroller:[viewcontroller New];
return YES;
}
2). In the-application:performactionforshortcutitem:completionhandler: method to implement the Click shortcut menu method:
How 3D touch triggers when you press a shortcut item on a program icon
-(void) application: (uiapplication) Application Performactionforshortcutitem: (Uiapplicationshortcutitem) Shortcutitem Completionhandler: (void (^) (BOOL)) Completionhandler
{
NSStringTitle
if ([Shortcutitem.localizedtitle isequaltostring:@ "hehe"])
{
[Email protected] "hehe";
}
else if ([Shortcutitem.localizedtitle isequaltostring:@ "hehe"])
{
[Email protected] "hehe";
}
else if ([Shortcutitem.localizedtitle isequaltostring:@ "Search"])
{
[Email protected] "search";
}
Here, let's play a little bit.
Since Uialertview is deprecated in iOS 9, Uialertcontroller is chosen
UialertcontrollerAlertcontroller=[uialertcontroller alertcontrollerwithtitle:@ "Tips"
Message:[nsstring stringwithformat:@ "You clicked on"%@ "", title "
Preferredstyle:uialertcontrollerstylealert];
UialertactionAction=[uialertaction actionwithtitle:@ "know."
Style:uialertactionstyledefault
handler:^ (UialertactionAction) {
[Alertcontroller Dismissviewcontrolleranimated:yes Completion:nil];
}];
[Alertcontroller addaction:action];
[Self.window.rootViewController Presentviewcontroller:alertcontroller
Animated:yes
Completion:nil];
}
2.3DTouch press operation within the program:
Table View
3DTouch Preview generated when pressing
The menu that appears when you pull up peek
1). First, implement a 3DTouch view controller to comply with:
The Uiviewcontrollerpreviewingdelegate protocol, which has 2 required-level protocol methods:
-previewingcontext:viewcontrollerforlocation:
And
-previewingcontext:commitviewcontroller:
2). Detects if 3DTouch is available and registers 3DTouch:
Detect if 3D touch is available
-(BOOL) is3dtouchavailiable
{
if (self.traitcollection.forcetouchcapability==uiforcetouchcapabilityavailable)
return YES;
return NO;
}
Register 3DTouch
if ([Self is3dtouchavailiable])
{
[Self registerforpreviewingwithdelegate:self sourceView:self.view];
}
3). Implementing the Protocol Method:
-(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;
Menu-------------------------------Pull-up
Sticky and click Logic
Uipreviewaction *topaction=[uipreviewaction actionwithtitle:@ "pinned" Style:uipreviewactionstyledefault handler:^ ( Uipreviewaction * Action, Uiviewcontroller * Previewviewcontroller) {
[Wkself.dataarray ExchangeObjectAtIndex:indexPath.row withobjectatindex:0];
[Wkself.tbvew Reloaddata];
[Wkself showalert:@ "hint" body:@ "is pinned"];
}];
Delete and its click logic
Uipreviewaction *deleteaction=[uipreviewaction actionwithtitle:@ "Delete" style:uipreviewactionstyledestructive handler : ^ (uipreviewaction *action, Uiviewcontroller * previewviewcontroller) {
[Wkself.dataarray RemoveObjectAtIndex:indexPath.row];
[Wkself.tbvew Reloaddata];
[Wkself showalert:@ "Warning" body:@ "deleted"];
}];
Pass the pull-up menu item to detail
[Email protected] [Topaction,deleteaction];
return detail;
}
return nil;
}
-(void) Previewingcontext: (id<uiviewcontrollerpreviewing>) Previewingcontext Commitviewcontroller: ( Uiviewcontroller *) Viewcontrollertocommit
{
[Self showviewcontroller:viewcontrollertocommit sender:self];
}
4). The view controller to be previewing must implement:
-(Nsarray<id<uipreviewactionitem>> *) Previewactionitems method, which defines the pull-out menu on peek:
/**peek when the menu is pulled up */
-(nsarray<id<uipreviewactionitem>> *) previewactionitems
{
return self.actions;
}
Https://github.com/whj111/3D_Touch_Demo
Basic use of iOS development--3d touch