美文网首页
iOS9之3D Touch

iOS9之3D Touch

作者: wxlan | 来源:发表于2015-11-03 17:46 被阅读0次

    一、主屏幕快速选项(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

    相关文章

      网友评论

          本文标题:iOS9之3D Touch

          本文链接:https://www.haomeiwen.com/subject/rxbqhttx.html