3D Touch

3D Touch 使用简介

  1. 准备工作

    (1)mac 10.11; xcode 7.1; simulator ios 9.1
    (2)配置simulator(用于支持3D-Touch)
    

    参照:SBShortcutMenuSimulator

  2. 示例

    (1)桌面3D-Touch(Home shortcut)


静态方式

1.打开工程info.plist
  添加如下信息:
<key>UIApplicationShortcutItems</key>
    <array>
        <dict>
        <key>UIApplicationShortcutItemIconType</key>
        <string>UIApplicationShortcutIconTypeShare</string>
            <key>UIApplicationShortcutItemTitle</key>
            <string>3d_touch</string>
            <key>UIApplicationShortcutItemType</key>
            <string>hello_touch</string>
        </dict>
    </array>
注:UIApplicationShortcutItems:桌面shortcut
   UIApplicationShortcutItemTitle:标题
   UIApplicationShortcutItemType:类型
 参照官方文档:
https://developer.apple.com/library/prerelease/ios/documentation/UIKit/Reference/UIApplicationShortcutItem_class/index.html

动态方式

AppDelegate

//配置shortcutItems
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    // Override point for customization after application launch.
    UIApplicationShortcutItem *shortItem1 = [[UIApplicationShortcutItem alloc] initWithType:@"Share" localizedTitle:@"分享"];
    UIApplicationShortcutItem *shortItem2 = [[UIApplicationShortcutItem alloc] initWithType:@"Hello" localizedTitle:@"测试"];
    NSArray *shortItems = [[NSArray alloc] initWithObjects:shortItem1, shortItem2, nil];
    NSLog(@"%@", shortItems);
    [[UIApplication sharedApplication] setShortcutItems:shortItems];
    return YES;
}
//在此响应点击事件
- (void)application:(UIApplication *)application performActionForShortcutItem:(UIApplicationShortcutItem *)shortcutItem completionHandler:(void (^)(BOOL))completionHandler{
    if ([shortcutItem.localizedTitle  isEqual: @"测试"]) {
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"3D" message:@"hello world" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
        [alert show];
        return;
    }
}

(2)预览(Peek and Pop)

Peek由一个可响应事件的View触发,默认是关闭的,我们需要通过控制器的registerForPreviewingWithDelegate: sourceView:方法注册,第一个参数为UIViewControllerPreviewingDelegate的代理,Peek触发轻压时会调用其previewingContext:viewControllerForLocation方法,重压时会调用previewingContext:commitViewController:方法。第二个参数为触发Peek事件的源视图
1.ViewController中

注册代理:UIViewControllerPreviewingDelegate
[self registerForPreviewingWithDelegate:self    sourceView:self.view];
实现代理方法:
//POP 重按
- (void)previewingContext:(id<UIViewControllerPreviewing>)previewingContext commitViewController:(UIViewController *)viewControllerToCommit{
    [self showViewController:viewControllerToCommit sender:self];
}

//显示Peek窗口 轻按
- (UIViewController *)previewingContext:(id<UIViewControllerPreviewing>)previewingContext viewControllerForLocation:(CGPoint)location{
    //TODO
    ImageViewController *ImageViewController = [[UIStoryboard storyboardWithName:@"Main" bundle:[NSBundle mainBundle]] instantiateViewControllerWithIdentifier:@"ImageViewController"];
    ImageViewController.preferredContentSize = CGSizeMake(0.0f,300);

    CGRect rect = CGRectMake(10, location.y - 10, self.view.frame.size.width - 20,20);
    previewingContext.sourceRect = rect;
    return ImageViewController;
}

猜你喜欢

转载自blog.csdn.net/Zhul520/article/details/49737113