3D Touch for iOS development

Source: Internet
Author: User
Tags home screen

1. Introduction

3DTouch is a technology of Apple after 6s, only in 6s and above the real machine operation, Xcode simulator is not supported.

    • Quick Actions (Click the icon's shortcut)

    • Peek&pop (in-app Quick preview content)

    • Uitouch and Livephoto (read pressure and maximum pressure)

2, Quick Actions (click the icon shortcut)

Click on the app's icon to pop up the Today widget (if any) and Uiapplicationshortcutitem shortcut-This is the quick actions!

////UIApplicationShortcutItem.h//UIKit////Copyright 2015-2017 Apple Inc. All rights reserved.//#import<Foundation/Foundation.h>Ns_assume_nonnull_begin@classuiimage;typedef ns_enum (Nsinteger, Uiapplicationshortcuticontype) {uiapplicationshortcuticontypecompose, 
    //User definableUiapplicationshortcuticontypeplay,//PlayUiapplicationshortcuticontypepause,//PauseUiapplicationshortcuticontypeadd,//AddUiapplicationshortcuticontypelocation,//locationUiapplicationshortcuticontypesearch,//SearchUiapplicationshortcuticontypeshare,//ShareUiapplicationshortcuticontypeprohibit Ns_enum_available_ios (9_1),//prohibitedUiapplicationshortcuticontypecontact Ns_enum_available_ios (9_1),//Contact PersonUiapplicationshortcuticontypehome Ns_enum_available_ios (9_1),//FamilyUiapplicationshortcuticontypemarklocation Ns_enum_available_ios (9_1),//Mark PositionUiapplicationshortcuticontypefavorite Ns_enum_available_ios (9_1),//PreferenceUiapplicationshortcuticontypelove Ns_enum_available_ios (9_1),//LoveUiapplicationshortcuticontypecloud Ns_enum_available_ios (9_1),//Cloud AccessUiapplicationshortcuticontypeinvitation Ns_enum_available_ios (9_1),//InviteUiapplicationshortcuticontypeconfirmation Ns_enum_available_ios (9_1),//ConfirmUiapplicationshortcuticontypemail Ns_enum_available_ios (9_1),//EmailUiapplicationshortcuticontypemessage Ns_enum_available_ios (9_1),//messageUiapplicationshortcuticontypedate Ns_enum_available_ios (9_1),//CalendarUiapplicationshortcuticontypetime Ns_enum_available_ios (9_1),//TimeUiapplicationshortcuticontypecapturephoto Ns_enum_available_ios (9_1),//PhotosUiapplicationshortcuticontypecapturevideo Ns_enum_available_ios (9_1),//VideoUiapplicationshortcuticontypetask Ns_enum_available_ios (9_1),//Create a taskuiapplicationshortcuticontypetaskcompleted Ns_enum_available_ios (9_1),//Complete the TaskUiapplicationshortcuticontypealarm Ns_enum_available_ios (9_1),//Alarm ClockUiapplicationshortcuticontypebookmark Ns_enum_available_ios (9_1),//BookmarkUiapplicationshortcuticontypeshuffle Ns_enum_available_ios (9_1),//ShuffleUiapplicationshortcuticontypeaudio Ns_enum_available_ios (9_1),//AudioUiapplicationshortcuticontypeupdate Ns_enum_available_ios (9_1)//Update} ns_enum_available_ios (9_0) __tvos_prohibited; Ns_class_available_ios (9_0) __tvos_prohibited@interfaceUiapplicationshortcuticon:nsobject <NSCopying>//Initialize+(Instancetype) Iconwithtype: (uiapplicationshortcuticontype) type;//Create a Home screen quick action icon based on an image in an application package, preferably in an asset catalog. + (Instancetype) Iconwithtemplateimagename: (NSString *) Templateimagename;@endNs_class_available_ios (9_0) __tvos_prohibited@interfaceUiapplicationshortcutitem:nsobject <nscopying, nsmutablecopying>//Initialize-(instancetype) init ns_unavailable;/** @param type custom String type @param localizedtitle title @param localizedsubtitle subtitle @param icon @param userInfo the information passed /c7>*/-(Instancetype) Initwithtype: (NSString *) type localizedtitle: (NSString *) Localizedtitle Localizedsubtitle: (Nullable NSString *) Localizedsubtitle icon: (Nullable Uiapplicationshortcuticon *) icon UserInfo: (Nullable nsdictionary *) UserInfo Ns_designated_initializer;-(Instancetype) Initwithtype: (NSString *) type localizedtitle: (NSString *) Localizedtitle;//Read related initialization information@property (nonatomic, copy,ReadOnly) NSString *type; @property (nonatomic, copy,ReadOnly) NSString *Localizedtitle, @property (nullable, nonatomic, copy,ReadOnly) NSString *Localizedsubtitle, @property (nullable, nonatomic, copy,ReadOnly) Uiapplicationshortcuticon *icon; @property (nullable, nonatomic, copy,ReadOnly) nsdictionary<nsstring *,ID<NSSecureCoding>> *UserInfo;@endNs_class_available_ios (9_0) __tvos_prohibited//Variable Item@interfaceUimutableapplicationshortcutitem:uiapplicationshortcutitem@property (nonatomic, copy) NSString*type; @property (nonatomic, copy) NSString*Localizedtitle, @property (nullable, nonatomic, copy) NSString*Localizedsubtitle, @property (nullable, nonatomic, copy) Uiapplicationshortcuticon*icon; @property (nullable, nonatomic, copy) Nsdictionary<nsstring *,ID<NSSecureCoding>> *UserInfo;@endNs_assume_nonnull_end
Uiapplicationshortcutitem related APIs

2.1. Static creation

Directly configure the Info.plist file

2.2. Dynamic creation

Code creation:

-(BOOL) Application: (UIApplication *) application didfinishlaunchingwithoptions: (Nsdictionary *) launchoptions {Nsmutablearray*arrshortcutitem = (Nsmutablearray *) [UIApplication sharedapplication].shortcutitems; Uiapplicationshortcutitem*SHOREITEM1 = [[Uiapplicationshortcutitem alloc] Initwithtype:@"to see the Book of coffee in the mansion"Localizedtitle:@"Search"Localizedsubtitle:nil Icon:[uiapplicationshortcuticon Iconwithtype:uiapplicationshortcuticontypesearch]    Userinfo:nil];    [Arrshortcutitem addobject:shoreitem1]; Uiapplicationshortcutitem*SHOREITEM2 = [[Uiapplicationshortcutitem alloc] Initwithtype:@"Master du is fast two open split"Localizedtitle:@"New News"Localizedsubtitle:@""Icon:[uiapplicationshortcuticon Iconwithtype:uiapplicationshortcuticontypecompose] userInfo:nil];    [Arrshortcutitem ADDOBJECT:SHOREITEM2]; Uiapplicationshortcutitem*SHOREITEM3 = [[Uiapplicationshortcutitem alloc] Initwithtype:@"SDFASJDFHSKSDJFSDAJKLFDJFH"Localizedtitle:@"Increased development"Localizedsubtitle:@""Icon:[uiapplicationshortcuticon iconwithtype:uiapplicationshortcuticontypelocation] userInfo:nil];    [Arrshortcutitem ADDOBJECT:SHOREITEM3]; Uiapplicationshortcutitem*SHOREITEM4 = [[Uiapplicationshortcutitem alloc] Initwithtype:@"SDFASJDFHSKDJFH"Localizedtitle:@"New"Localizedsubtitle:@""Icon:[uiapplicationshortcuticon Iconwithtype:uiapplicationshortcuticontypeshare] userInfo:nil];    [Arrshortcutitem ADDOBJECT:SHOREITEM4]; [UIApplication Sharedapplication].shortcutitems=Arrshortcutitem; returnYES;}

2.3. Click Response method

-(void) Application: (UIApplication *) application Performactionforshortcutitem: (Uiapplicationshortcutitem *) Shortcutitem Completionhandler: (void (^) (BOOL)) completionhandler{    NSLog (@ "  Name ==%@\n type =  %@", Shortcutitem.localizedtitle,shortcutitem.type);}

 PS: System first load static method created by item, can be mixed loaded, up to 4 item;

After the official release there will be 5 item, Apple Unified Add a Shared Item;

Some of the item's icons appear on the right side, which is related to the app's position on the screen;

3, Peek&pop (in-app Quick preview content)

Peek Page preview: Add a little pressure to the view, the target interface will be previewed as a frame;

Pop Pull out Menu: In the interface preview, if there is a menu option, drag up below the menu bar will appear;

Continue to increase the pressure will jump to the target interface--that is peek&pop!

3.1. Peek

This example adds this feature to the picture: if one is interactive, two is registered Registerforpreviewingwithdelegate

    [Self.imageview setuserinteractionenabled:yes];     if (self.traitCollection.forceTouchCapability = = uiforcetouchcapabilityavailable) {        [self Registerforpreviewingwithdelegate: (ID) self sourceView:self.imageView];    }

To create a menu bar: You need to rewrite-(nsarray<id<uipreviewactionitem>> *) Previewactionitems in the target Viewcontroller interface;

-(nsarray<ID<UIPreviewActionItem>> *) previewactionitems{uipreviewaction*action1 = [Uipreviewaction actionwithtitle:@"Action1"Style:uipreviewactionstyledefault handler:^ (uipreviewaction * _nonnull action, Uiviewcontroller *_nonnull Previewviewcontroller) {NSLog (@"Action1");        }]; Uipreviewaction*action2 = [Uipreviewaction actionwithtitle:@"Action2"style:uipreviewactionstyleselected handler:^ (uipreviewaction * _nonnull action, Uiviewcontroller *_nonnull Previewviewcontroller) {NSLog (@"Action2");    }]; Nsarray*actions =@[action1,action2]; Uipreviewactiongroup*group1 = [Uipreviewactiongroup actiongroupwithtitle:@"Action Group"Style:uipreviewactionstyledefault Actions:actions]; return@[action1,action2,group1];}

Preview view methods and jump View methods:

//Preview-(Nullable Uiviewcontroller *) Previewingcontext: (ID<UIViewControllerPreviewing>) Previewingcontext viewcontrollerforlocation: (cgpoint) Location Ns_available_ios (9_0) {if([Self.presentedviewcontroller Iskindofclass:[videoviewcontrollerclass]]){        returnNil; }Else{Videoviewcontroller*CONTENTVC =[[Videoviewcontroller alloc] init]; returnCONTENTVC; }}//re-press ENTER- (void) Previewingcontext: (ID<UIViewControllerPreviewing>) Previewingcontext Commitviewcontroller: (Uiviewcontroller *) Viewcontrollertocommit Ns_available_ios (9_0) {NSLog (@"re-press ENTER"); Videoviewcontroller*v =[[Videoviewcontroller alloc] init]; [Self presentviewcontroller:v animated:yes completion:nil];}

4, Uitouch and Livephoto (read pressure and maximum pressure)

There are two methods in the Uitouch class to read real-time pressure and maximum pressure:

// Touch Pressure value @property (nonatomic,readonly) cgfloat Force Ns_available_ios (9_0); // Maximum touch pressure value @property (nonatomic,readonly) cgfloat maximumpossibleforce Ns_available_ios (9_0);

In this example, the ImageView proxy method is overridden to obtain these two values:

-(void) touchesmoved: (Nsset<uitouch *> *) touches withevent: (uievent *)event{    NSLog (  @ "=======%lf", Touches.anyObject.force);} -(void) touchesended: (Nsset<uitouch *> *) touches withevent: (uievent *)event{    NSLog (@ "------%lf", Touches.anyObject.maximumPossibleForce);}

Results:

Livephoto is a picture and a 3-second video that plays this video when you use 3D touch, and the finger release ends the playback.

So this effect can be done by uitouch the above two attributes!

3D Touch

for iOS development

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.