文章目录
  1. 1. 根据apple官方3D touch的例子写个3D Touch功能
    1. 1.1. 一,3D touch sample下载
      1. 1.1.1. 1) 要使用3D touch 需要满足两个必要条件:
      2. 1.1.2. 2) 如何设置(静态设置)
      3. 1.1.3. 3)动态设置
      4. 1.1.4. 自定义设置3D touch图标
    2. 1.2. 二,3D touch的peek&pop

根据apple官方3D touch的例子写个3D Touch功能

一,3D touch sample下载

1.
下载地址(文档)

  1. 下载后打开ApplicationShortcuts,发现是swift实现的,现在我们来按照官方文档的说话,再参照官方给的sample项目进行学习
1) 要使用3D touch 需要满足两个必要条件:
  • ios9以上系统
  • iphone6以上
2) 如何设置(静态设置)

根据apple官方文档api的介绍:(Define static quick actions in your app’s Info.plist file in the UIApplicationShortcutItems array.)也就是在info.plist中有的UIApplicationShortcutItems节点下面需要定义快捷按钮(图标用的是系统的):

info.plist:

<key>UIApplicationShortcutItems</key>
  <array>
      <dict>
          <key>UIApplicationShortcutItemIconType</key>
          <string>UIApplicationShortcutIconTypeSearch</string>
          <key>UIApplicationShortcutItemSubtitle</key>
          <string>shortcutSubtitle1</string>
          <key>UIApplicationShortcutItemTitle</key>
          <string>shortcutTitle1</string>
          <key>UIApplicationShortcutItemType</key>
          <string>$(PRODUCT_BUNDLE_IDENTIFIER).First</string>
          <key>UIApplicationShortcutItemUserInfo</key>
          <dict>
              <key>firstShorcutKey1</key>
              <string>firstShortcutKeyValue1</string>
          </dict>
      </dict>
      <dict>
          <key>UIApplicationShortcutItemIconType</key>
          <string>UIApplicationShortcutIconTypeShare</string>
          <key>UIApplicationShortcutItemSubtitle</key>
          <string>shortcutSubtitle2</string>
          <key>UIApplicationShortcutItemTitle</key>
          <string>shortcutTitle2</string>
          <key>UIApplicationShortcutItemType</key>
          <string>$(PRODUCT_BUNDLE_IDENTIFIER).Second</string>
          <key>UIApplicationShortcutItemUserInfo</key>
          <dict>
              <key>secondShortcutKey1</key>
              <string>secondShortcutValue1</string>
          </dict>
      </dict>
  </array>

appDelegate impl:

  • obj-c:
-(void)application:(UIApplication *)application performActionForShortcutItem:(UIApplicationShortcutItem *)shortcutItem completionHandler:(void (^)(BOOL))completionHandler{


}
  • swift:
func application(application: UIApplication, performActionForShortcutItem shortcutItem: UIApplicationShortcutItem, completionHandler: Bool -> Void){

}
3)动态设置
  • obj-c:
UIApplication *app = [UIApplication sharedApplication]; 
//1.获取shortcutItems
NSArray *existingShortcutItems = [app shortcutItems];

//2.获取第0个shortcutItem  
UIApplicationShortcutItem *oldItem = [existingShortcutItems objectAtIndex: 0];  

//3.更新shortcutItems
//将旧的shortcutItem改变为可修改类型shortcutItem  
UIMutableApplicationShortcutItem *mutableItem = [oldItem mutableCopy];  

//修改shortcutItem的显示标题  
[mutableItem setLocalizedTitle: @“Share”];

//根据旧的shortcutItems生成可变shortcutItems数组  
NSMutableArray *updatedShortcutItems = [existingShortcutItems mutableCopy];  

//修改可变shortcutItems数组中对应index下的元素为新的shortcutItem  
[updatedShortcutItems replaceObjectAtIndex: 0 withObject: mutableItem];  

//修改应用程序对象的shortcutItems为新的数组  
[app setShortcutItems: updatedShortcutItems];
  • swift:

 let shortcut3 = UIMutableApplicationShortcutItem(type: "Third", localizedTitle: "Play", localizedSubtitle: "Will Play an item", icon: UIApplicationShortcutIcon(type: .Play), userInfo: [
                    AppDelegate.applicationShortcutUserInfoIconKey: UIApplicationShortcutIconType.Play.rawValue
                ]
            )
            
            let shortcut4 = UIMutableApplicationShortcutItem(type: "Third", localizedTitle: "Pause", localizedSubtitle: "Will Pause an item", icon: UIApplicationShortcutIcon(type: .Pause), userInfo: [
                    AppDelegate.applicationShortcutUserInfoIconKey: UIApplicationShortcutIconType.Pause.rawValue
                ]
            )
            
            // Update the application providing the initial 'dynamic' shortcut items.
            application.shortcutItems = [shortcut3, shortcut4]
            
自定义设置3D touch图标

官方文档给出三个必要条件:

1.使用UIApplicationShortcutItemIconFile类型声明

2. 提供的图片必须是35*35的像素
3. 图片的颜色必须是单色(single color)

sample:

<dict>
            <key>UIApplicationShortcutItemIconFile</key>
            <string>customer_3DTouch</string>
            <key>UIApplicationShortcutItemSubtitle</key>
            <string>shortcutSubtitle3</string>
            <key>UIApplicationShortcutItemTitle</key>
            <string>shortcutTitle3</string>
            <key>UIApplicationShortcutItemType</key>
            <string>$(PRODUCT_BUNDLE_IDENTIFIER).Second</string>
            <key>UIApplicationShortcutItemUserInfo</key>
            <dict>
                <key>thirdShortcutKey1</key>
                <string>thirdShortcutValue1</string>
            </dict>
        </dict>
                     

customer_3DTouch:
customer_3DTouch

二,3D touch的peek&pop

实现3D touch的peek和pop需要三步:

  1. ios9以后在UIViewController里添加了一个UIViewControllerPreviewingDelegate协议,需要实现这个delegate
  2. 注册开启使用peek&pop,在哪里用,sourceView就写哪个view
[self registerForPreviewingWithDelegate:self sourceView:homeCell];

3, 重载两个方法

- (nullable UIViewController *)previewingContext:(id <UIViewControllerPreviewing>)previewingContext viewControllerForLocation:(CGPoint)location{
    
     Show3DViewController *childVC = (Show3DViewController *)InstantiateControllerFromXIB([Show3DViewController class]);
//     UIViewController *childVC = [[UIViewController alloc] init];
    
    childVC.preferredContentSize = CGSizeMake(0.0f,300.0f);
    
    CGRect rect = CGRectMake(10, location.y - 10, self.view.frame.size.width - 20,20);
    previewingContext.sourceRect = rect;
    return childVC;
}


- (void)previewingContext:(id <UIViewControllerPreviewing>)previewingContext commitViewController:(UIViewController *)viewControllerToCommit{
    
     [self showViewController:viewControllerToCommit sender:self]; 
}
文章目录
  1. 1. 根据apple官方3D touch的例子写个3D Touch功能
    1. 1.1. 一,3D touch sample下载
      1. 1.1.1. 1) 要使用3D touch 需要满足两个必要条件:
      2. 1.1.2. 2) 如何设置(静态设置)
      3. 1.1.3. 3)动态设置
      4. 1.1.4. 自定义设置3D touch图标
    2. 1.2. 二,3D touch的peek&pop