iOS system 3DTouch full resolution

Source: Internet
Author: User
Tags home screen

iOS system 3DTouch full resolution 

Overview

IOS10 system Login In China, the use of 3D touch is more frequent in the system, so it is necessary to do some understanding of the 3D touch function introduced in iOS9.

In daily development, we often need to use two features in 3D touch

    1. Use 3DTouch action on the app icon on the home screen

    2. Using 3DTouch operations on a control within an application

Shortcutitem Overview

The Shortcutitem feature allows the user to use 3DTouch on the app icon on the home screen, and if the operation is valid, a few quick options are given to allow the user to operate

Static add

Add the Uiapplicationshortcutitems keyword in info.plist to configure it as follows

The definitions of each of these keywords are as follows:

Uiapplicationshortcutitemtype: Specific string of shortcut options (required)
Uiapplicationshortcutitemtitle: Title of the shortcut option (required)
Uiapplicationshortcutitemsubtitle: Sub-title for shortcut options (optional)
Uiapplicationshortcutitemicontype: Icon for shortcut options (optional)
Uiapplicationshortcutitemiconfile: Custom icon for shortcut options (optional)
Uiapplicationshortcutitemuserinfo: Additional Information for shortcut options (optional)

Add Uiapplicationshortcutitem Dynamically

Each shortcut option is a Uiapplicationshortcutitem object with the specified initializer (Ns_designated_initializer) as follows

-(Instancetype) Initwithtype: (NSString *) type localizedtitle: (NSString *) Localizedtitle Localizedsubtitle: (Nullable NSString *) Localizedsubtitle icon: (Nullable Uiapplicationshortcuticon *) icon UserInfo: (Nullable nsdictionary *) UserInfo;

The explanations of each parameter are as follows:

Type: A specific string of shortcut options (required)
Localizedtitle: Title of the shortcut option (required)
Localizedsubtitle: Sub-title for shortcut options (optional)
Icon: Icons for shortcut options (optional)
UserInfo: Additional Information for shortcut options (optional)

Uiapplicationshortcuticon

Each shortcut icon is a Uiapplicationshortcuticon object, we can use multiple system-provided icons, or we can customize our own icons

Use system-provided icons + (Instancetype) Iconwithtype: (uiapplicationshortcuticontype) type;//custom icon + (instancetype) Iconwithtemplateimagename: (NSString *) templateimagename;

The system provides the following icon styles:

Note: The custom icon needs to use the skeleton icon, and 1 time times the icon size is recommended 35*35

Example
1234567891011121314 // 以"动态添加四个快捷可选项"为例UIApplicationShortcutIcon *searchShortcutIcon = [UIApplicationShortcutIcon iconWithType:UIApplicationShortcutIconTypeSearch];UIApplicationShortcutItem *searchShortcutItem = [[UIApplicationShortcutItem alloc] initWithType:@"com.liupeng.search"localizedTitle:@"Search"localizedSubtitle:@"Search Subtitle"icon:searchShortcutIcon userInfo:nil];UIApplicationShortcutIcon *playShortcutIcon = [UIApplicationShortcutIcon iconWithType:UIApplicationShortcutIconTypePlay];UIApplicationShortcutItem *playShortcutItem = [[UIApplicationShortcutItem alloc] initWithType:@"com.liupeng.play"localizedTitle:@"Play"localizedSubtitle:@"Play Subtitle"icon:playShortcutIcon userInfo:nil];UIApplicationShortcutIcon *qrShortcutIcon = [UIApplicationShortcutIcon iconWithTemplateImageName:@"shortcut_QR"];UIApplicationShortcutItem *qrShortcutItem = [[UIApplicationShortcutItem alloc] initWithType:@"com.liupeng.qr"localizedTitle:@"QR" localizedSubtitle:@"QR Subtitle"icon:qrShortcutIcon userInfo:nil];UIApplicationShortcutIcon *payShortcutIcon = [UIApplicationShortcutIcon iconWithTemplateImageName:@"shortcut_pay"];UIApplicationShortcutItem *payShortcutItem = [[UIApplicationShortcutItem alloc] initWithType:@"com.liupeng.pay"localizedTitle:@"Pay"localizedSubtitle:@"Pay Subtitle"icon:payShortcutIcon userInfo:nil];[UIApplication sharedApplication].shortcutItems = @[searchShortcutItem, playShortcutItem, qrShortcutItem, payShortcutItem];
Trigger Callback

When the user enters the application by clicking on the shortcut option, the following method can be called back, which allows us to differentiate it by the type of the shortcut option for different operations

123456789101112 - (void)application:(UIApplication *)application performActionForShortcutItem:(UIApplicationShortcutItem *)shortcutItem completionHandler:(void (^)(BOOL))completionHandler{   if ([shortcutItem.type isEqualToString:@"com.liupeng.search"])   {       // do something ...   }   elseif([shortcutItem.type isEqualToString:@"com.liupeng.play"])   {       // do something ...   }   // ...}
Attention

Before adding shortcut options dynamically, you need to determine if 3D touch is supported to avoid running programs on unsupported devices to cause a flash

1234567891011121314 if([self respondsToSelector:@selector(traitCollection)]){   if([self.traitCollection respondsToSelector:@selector(forceTouchCapability)])   {       if(self.traitCollection.forceTouchCapability == UIForceTouchCapabilityAvailable)       {           // 支持3D Touch       }       else       {           // 不支持3D Touch       }   }}

Note: On a device that supports 3D touch, users can turn off the 3D touch feature by setting up the universal-----accessibility---3D touch during program operation, so we need to rewrite the-traitcollectiondidchange: method to handle at any time

Peek & Pop Overview

Peek and Pop is a new interactive pattern within the app, and when the user is constantly adding strength to the controls, it moves into four stages

    • Tap the control, except for the controls that trigger peek, all of the other areas are empty

    • Continue to force Peek is triggered to show pop interface snapshot

    • Swipe up to show shortcut options

    • Continue to force jump into the pop interface

Use

The use of Peek & Pop is explained in the example "Uilabel control from Viewcontroller through Peek & Pop entry Detailviewcontroller"

    • controllers that need to implement peek & pop interactions adhere to the Uiviewcontrollerpreviewingdelegate protocol

12 @interface ViewController () <uiviewcontrollerpreviewingdelegate>@end</uiviewcontrollerpreviewingdelegate>
    • Register Peek & POP functionality within the controller for controls that need to implement peek & pop interactions

1 [self registerForPreviewingWithDelegate:self sourceView:label];
    • When entering Peek State, the system will callback the following method

123456789101112 - (nullable UIViewController *)previewingContext:(id <uiviewcontrollerpreviewing>)previewingContext viewControllerForLocation:(CGPoint)location{   // previewingContext.sourceView: 触发Peek & Pop操作的视图   // previewingContext.sourceRect: 设置触发操作的视图的不被虚化的区域   DetailViewController *detailVC = [[DetailViewController alloc] init];   // 预览区域大小(可不设置)   detailVC.preferredContentSize = CGSizeMake(0, 300);   returndetailVC;}</uiviewcontrollerpreviewing>
    • When entering the pop state, the system will callback the following method

1234 - (void)previewingContext:(id <uiviewcontrollerpreviewing>)previewingContext commitViewController:(UIViewController *)viewControllerToCommit{   [self.navigationController pushViewController:viewControllerToCommit animated:YES];}</uiviewcontrollerpreviewing>
    • You want to provide some quick options in peek, you need to rewrite the Previewactionitems getter method in Detailviewcontroller

123456789101112131415161718 - (NSArray<id<uipreviewactionitem>> *)previewActionItems{   UIPreviewAction *action1 = [UIPreviewAction actionWithTitle:@"选项一"style:UIPreviewActionStyleDefault handler:^(UIPreviewAction * _Nonnull action, UIViewController * _Nonnull previewViewController) {   }];   UIPreviewAction *action2 = [UIPreviewAction actionWithTitle:@"选项二" style:UIPreviewActionStyleSelected handler:^(UIPreviewAction * _Nonnull action, UIViewController * _Nonnull previewViewController) {   }];   UIPreviewAction *action3 = [UIPreviewAction actionWithTitle:@"选项三"style:UIPreviewActionStyleDestructive handler:^(UIPreviewAction * _Nonnull action, UIViewController * _Nonnull previewViewController) {   }];    UIPreviewActionGroup *actionGroup = [UIPreviewActionGroup actionGroupWithTitle:@"选项组"style:UIPreviewActionStyleDefault actions:@[action1, action2]];   return@[action1, action2, action3, actionGroup];}</id<uipreviewactionitem>

 

iOS system 3DTouch full resolution

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.