iOS9之3D Touch

一、主屏幕快速选项(Home Screen Quick Actions)

主屏幕快速选项就是指在主屏幕上按压应用icon时出现一系列的快捷选项,如图说示:

IMG_0287.png

实现主屏幕快速选项有2种方式:静态快速选项和动态快速选项

  • 1.0 静态快速选项
    在info.plist文件中定义UIApplicationShortcutItems数组
    <key>UIApplicationShortcutItems</key>
    <array>
    <dict>
    <key>UIApplicationShortcutItemIconFile</key> // 自定义图片 可选属性
    <string>share.png</string> // 图片名

    <key>UIApplicationShortcutItemSubtitle</key> // 副标题 可选属性
    <string>这是分享</string>
    <key>UIApplicationShortcutItemTitle</key> // 主标题 必须有
    <string>分享</string>
    <key>UIApplicationShortcutItemType</key> // item表示 必须有
    <string>3dtouch.share</string>
    <key>UIApplicationShortcutItemUserInfo</key> // 数据 可选
    <dict>
    <key>url</key>
    <string>index</string>
    </dict>
    </dict>
    </array>

  • 2.0 动态快速选项
    在iOS9中,UIApplication中增加了shortcutItems的属性,
    @class UIApplicationShortcutItem;
    @interface UIApplication (UIShortcutItems)
    // Register shortcuts to display on the home screen, or retrieve currently registered shortcuts.
    @property (nullable, nonatomic, copy) NSArray<UIApplicationShortcutItem *> *shortcutItems NS_AVAILABLE_IOS(9_0);
    @end
    UIApplicationShortcutItem就是我们创建的每一个快速选项,放到shortcutItems数组中就可以了,我们可以在- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(nullable NSDictionary *)launchOptions NS_AVAILABLE_IOS(3_0);方法中代码创建,代码如下:
    - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    // UIApplicationShortcutIcon *icon = [UIApplicationShortcutIcon iconWithTemplateImageName:@"down"]; 使用自定义图片icon
    UIApplicationShortcutIcon *icon = [UIApplicationShortcutIcon iconWithType:UIApplicationShortcutIconTypeSearch]; // 使用系统icon
    UIApplicationShortcutItem *item1 = [[UIApplicationShortcutItem alloc] initWithType:@"3dtouch.search" localizedTitle:@"搜索" localizedSubtitle:@"11" icon:icon userInfo:nil];
    // UIApplicationShortcutItem *item1 = [[UIApplicationShortcutItem alloc] initWithType:@"3dtouch.home" localizedTitle:@"搜索"];
    [[UIApplication sharedApplication] setShortcutItems:@[item1]];
    // Override point for customization after application launch.
    return YES;
    }
    我们来看看UIApplicationShortcutItem这个类
    @interface UIApplicationShortcutItem : NSObject <NSCopying, NSMutableCopying>

    - (instancetype)init NS_UNAVAILABLE;
    - (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;
    
    // An application-specific string that identifies the type of action to perform.
    @property (nonatomic, copy, readonly) NSString *type;
    
    // Properties controlling how the item should be displayed on the home screen.
    @property (nonatomic, copy, readonly) NSString *localizedTitle;
    @property (nullable, nonatomic, copy, readonly) NSString       *localizedSubtitle;
    @property (nullable, nonatomic, copy, readonly) UIApplicationShortcutIcon *icon;
    
    // Application-specific information needed to perform the action.
    // Will throw an exception if the NSDictionary is not plist-encodable.
    @property (nullable, nonatomic, copy, readonly) NSDictionary<NSString *, id <NSSecureCoding>> *userInfo;
    
    @end
    

UIApplicationShortcutItem有3种创建方式,我们主要使用用第二种和第三种方式,第二种方式参数比较全面,有些可以为空有些不能为空,第三种方式的参数都是必填,至于哪些参数为必须,哪些是非必须的,与静态快速选项是一致的,可以参照上面静态快速选项的介绍。在创建UIApplicationShortcutItem时,如果需要icon,我们会使用到UIApplicationShortcutIcon
@class UIImage;

    typedef NS_ENUM(NSInteger, UIApplicationShortcutIconType) {
        UIApplicationShortcutIconTypeCompose,
        UIApplicationShortcutIconTypePlay,
        UIApplicationShortcutIconTypePause,
        UIApplicationShortcutIconTypeAdd,
        UIApplicationShortcutIconTypeLocation,
        UIApplicationShortcutIconTypeSearch,
        UIApplicationShortcutIconTypeShare
    } NS_ENUM_AVAILABLE_IOS(9_0); // 系统提供icon

    NS_CLASS_AVAILABLE_IOS(9_0)
    @interface UIApplicationShortcutIcon : NSObject <NSCopying>

    // Create an icon using a system-defined image.
    + (instancetype)iconWithType:(UIApplicationShortcutIconType)type; // 使用系统icon

    // Create an icon from a custom image.
    // The provided image named will be loaded from the app's bundle
    // and will be masked to conform to the system-defined icon style.
    + (instancetype)iconWithTemplateImageName:(NSString *)templateImageName; // 使用自定义icon

    @end

这样我们的主屏幕快速选项就基本完成了,主屏幕快速选项静态和动态创建的总数量不能超过4个,超过部分会不显示,而且静态创建的会优先创建出来。快速选项样式不能自定义,只能是使用系统样式,如果你想图片固定在文字左边,或是文字加颜色或换行之类的需求是做不到的。

  • 3.0 从快速选项进入应用
    iOS9新增方法- (void)application:(UIApplication *)application performActionForShortcutItem:(UIApplicationShortcutItem *)shortcutItem completionHandler:(void(^)(BOOL succeeded))completionHandler NS_AVAILABLE_IOS(9_0);使我们可以从快速选项进入应用
    - (void)application:(UIApplication *)application performActionForShortcutItem:(nonnull UIApplicationShortcutItem *)shortcutItem completionHandler:(nonnull void (^)(BOOL))completionHandler
    {
    if ([shortcutItem.type isEqualToString:@"3dtouch.share"]) { // 判断点击的是哪个快速选项
    NSLog(@"进入分享");
    UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
    UINavigationController *controller = [storyboard instantiateViewControllerWithIdentifier:@"main"];

      ShareViewController *shareVC = [storyboard instantiateViewControllerWithIdentifier:@"ShareViewController"];
      [controller pushViewController:shareVC animated:YES];
      self.window.rootViewController = controller;
      [self.window makeKeyAndVisible];
      }
    

二、peek和pop

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 专著://www.greatytc.com/p/3443a3b27b2d 1.简单的介绍一下3D Touc...
    violafa阅读 1,042评论 1 0
  • 一、屏幕图标使用3D Touch创建快速进入入口: 1、与之相关的类: (1)、 UIApplicationSho...
    寻形觅影阅读 793评论 0 0
  • 3D Touch简介 2015年,苹果发布了iOS9以及iphone6s/iphone6s Plus,其中最具有创...
    简简蜗牛阅读 634评论 0 0
  • 我姐十二岁的时候,我妈生下了我,为什么呢,因为我爸喜欢男孩子,后来听说当他看到才出生的我又是个女孩的时候,他气的...
    小小周的大大梦阅读 327评论 6 0
  • are your time 稍大的风,夹杂可忽略的雨。 此刻,21:26..提笔,踌躇又好奇。 一意觉得写下点什么...
    MadOrange阅读 179评论 0 0