iOS development - 3D Touch application of a series - Quick Actions to create a desktop shortcut Icon

A statement

He has not made essays, and there are now more than a year. Also I want to continue to write essays period, but was delayed due to various reasons. Recently thought for a moment, there are still many things you want to write, you want to share, you want something recorded. Then I will continue to write essays, but not only for iOS direction, but also want to experience new things. In this much to say, we start reading the text of it.

Brief introduction

On supported 3D Touch devices, Quick Actions allows users to quickly and fewer steps to accomplish what they usually do, where so much can be done directly from the main screen. Such as micro hard to press the letter icon will pop up, such as "sweep swept away" and "My two-dimensional code" and other common functions, which I often used in the case of adding a friend, not a "sweep" of others is to be someone else. " sweep". Then this is its official presentation, 3D Touch - iOS - the Apple Developer .

Demo of this article has been placed on GitHub, Demo go GarveyCalvin / iOS-Travel download.

Supported devices from iOS 9.0 is the start to support 3D Touch in iPhone 6S and 6S Plus and iPhone series of follow-up out of the system.

Info.plist Create Shortcut

Shortcuts defined in the Info.plist direct support, the primary key is UIApplicationShortcutItemsthat it is an array Array. Key values each can view the Information Property List Key Reference , here a brief introduction about the role of each Key.

Key effect
UIApplicationShortcutItemType (required) Uniquely identifies
UIApplicationShortcutItemTitle (required) The title of the show
UIApplicationShortcutItemSubtitle (optional) The subtitle display
UIApplicationShortcutItemIconType (optional) Icon using the system
UIApplicationShortcutItemIconFile (optional) Using the Project icon
UIApplicationShortcutItemUserInfo (optional) Additional information

The following is a detailed example of the above-described UIApplicationShortcutItemUserInfo Key addition, the other are achieved.

Now we look at operating results, it is worth mentioning that, in the case of icons on the screen half and the lower half of the screen, displaying the sort that will be different, as shown in the following specific operating results.

Icon in the upper half of the screen

Icon in the lower half screen

Which determines whether the user clicks the shortcut

By implementing proxy method UIApplicationDelegate in AppDelegate.m file application:performActionForShortcutItem:completionHandler:, which the user can know a shortcut from coming in, specific code as follows.

- (void)application:(UIApplication *)application performActionForShortcutItem:(UIApplicationShortcutItem *)shortcutItem completionHandler:(void (^)(BOOL))completionHandler  API_AVAILABLE(ios(9.0)) {
    NSLog(@"%s", __FUNCTION__);
    
    if ([shortcutItem.type isEqualToString:@"search"]) {
        NSLog(@"用户从快捷方式“搜索”进来的");
    } else if ([shortcutItem.type isEqualToString:@"list"]) {
        NSLog(@"用户从快捷方式“榜单”进来的");
    } else if ([shortcutItem.type isEqualToString:@"public"]) {
        NSLog(@"用户从快捷方式“一键发布”进来的");
    }
}

Code Create Shortcut

Shortcuts also supports code creation, each UIApplicationShortcutItem is a shortcut, the general method is implemented in AppDelegate.m file application:didFinishLaunchingWithOptions:write the implementation code, the code easier to understand, there is no longer explained.

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    NSLog(@"%s", __FUNCTION__);
    
    if (@available(iOS 9.0, *)) {
        UIApplicationShortcutIcon *searchIcon = [UIApplicationShortcutIcon iconWithType:UIApplicationShortcutIconTypeSearch];
        UIApplicationShortcutItem *search = [[UIApplicationShortcutItem alloc] initWithType:@"search" localizedTitle:@"搜索" localizedSubtitle:nil icon:searchIcon userInfo:nil];
        
        UIApplicationShortcutIcon *publicIcon = [UIApplicationShortcutIcon iconWithTemplateImageName:@"upload"];
        UIApplicationShortcutItem *public = [[UIApplicationShortcutItem alloc] initWithType:@"public" localizedTitle:@"一键发布" localizedSubtitle:nil icon:publicIcon userInfo:nil];
        
        UIApplicationShortcutItem *list = [[UIApplicationShortcutItem alloc] initWithType:@"list" localizedTitle:@"榜单" localizedSubtitle:@"全区排行" icon:nil userInfo:nil];
        
        application.shortcutItems = @[list, public, search];
    }
    
    return YES;
}

data

Demo of this article has been placed on GitHub, Demo go GarveyCalvin / iOS-Travel download.

surroundings

Xcode: Version 10.1 (10B61)

Test models: iPhone XS Max

Test System: iOS 12.1

to sum up

Whether you are creating Quick Actions in info.plist or code are relatively simple, nothing complicated logic, it is easy to learn.

Quick Actions 的使用情况还是比较多的,就我而言,我经常会用支付宝和微信放出来的"扫一扫",支付宝的"乘车码"等等。

3D Touch 的应用范围不止这些,还有 Peek and Pop 和 Pressure Sensitivity,在以后或许会补上这系列的文章。

声明

博文作者:GarveyCalvin
博文出处:http://www.cnblogs.com/GarveyCalvin/
本文版权归作者和博客园共有,欢迎转载,但须保留此段声明,并给出原文链接,谢谢合作!

Guess you like

Origin www.cnblogs.com/GarveyCalvin/p/3d-touch-series-quick-action.html