Basic use of iOS development--3d touch

Source: Internet
Author: User


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
Uialertcontroller
Alertcontroller=[uialertcontroller alertcontrollerwithtitle:@ "Tips"
Message:[nsstring stringwithformat:@ "You clicked on"%@ "", title "
Preferredstyle:uialertcontrollerstylealert];
UialertactionAction=[uialertaction actionwithtitle:@ "know."
Style:uialertactionstyledefault
handler:^ (Uialertaction
Action) {
[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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.