3D Touch

作者: 昵什么称 | 来源:发表于2019-11-12 23:17 被阅读0次

icon 图标效果的添加

icon 图标的3D Touch 效果添加可以通过 plist 静态添加与代码动态添加两种方法。
静态添加
静态添加是在plist文件中添加内容的方式添加3D Touch。
也可以使用source code打开plist添加代码

<key>UIApplicationShortcutItems</key>
    <array>
        <dict>
            <key>UIApplicationShortcutItemIconFile</key>
            <string>setting_mor.png</string>
            <key>UIApplicationShortcutItemTitle</key>
            <string>设置</string>
            <key>UIApplicationShortcutItemType</key>
            <string>btn1</string>
        </dict>
    </array>

其中
UIApplicationShortcutItemTitle 标签的标题
UIApplicationShortcutItemType 标签的类型
这两项是必须设置的,
UIApplicationShortcutItemIconFile 图标文件
UIApplicationShortcutItemIconType 图表类型
UIApplicationShortcutItemSubtitle 副标题
UIApplicationShortcutItemUserInfo 信息字典
是可以选择设置的。
其中UIApplicationShortcutItemIconType(图片)的枚举为
typedef NS_ENUM(NSInteger, UIApplicationShortcutIconType) {
UIApplicationShortcutIconTypeCompose,
UIApplicationShortcutIconTypePlay,
UIApplicationShortcutIconTypePause,
UIApplicationShortcutIconTypeAdd,
UIApplicationShortcutIconTypeLocation,
UIApplicationShortcutIconTypeSearch,
UIApplicationShortcutIconTypeShare,
UIApplicationShortcutIconTypeProhibit, NS_ENUM_AVAILABLE_IOS(9_1),
UIApplicationShortcutIconTypeContact NS_ENUM_AVAILABLE_IOS(9_1),
UIApplicationShortcutIconTypeHome NS_ENUM_AVAILABLE_IOS(9_1),
UIApplicationShortcutIconTypeMarkLocation NS_ENUM_AVAILABLE_IOS(9_1),
UIApplicationShortcutIconTypeFavorite NS_ENUM_AVAILABLE_IOS(9_1),
UIApplicationShortcutIconTypeLove NS_ENUM_AVAILABLE_IOS(9_1),
UIApplicationShortcutIconTypeCloud NS_ENUM_AVAILABLE_IOS(9_1),
UIApplicationShortcutIconTypeInvitation NS_ENUM_AVAILABLE_IOS(9_1),
UIApplicationShortcutIconTypeConfirmation NS_ENUM_AVAILABLE_IOS(9_1),
UIApplicationShortcutIconTypeMail NS_ENUM_AVAILABLE_IOS(9_1),
UIApplicationShortcutIconTypeMessage NS_ENUM_AVAILABLE_IOS(9_1),
UIApplicationShortcutIconTypeDate NS_ENUM_AVAILABLE_IOS(9_1),
UIApplicationShortcutIconTypeTime NS_ENUM_AVAILABLE_IOS(9_1),
UIApplicationShortcutIconTypeCapturePhoto NS_ENUM_AVAILABLE_IOS(9_1),
UIApplicationShortcutIconTypeCaptureVideo NS_ENUM_AVAILABLE_IOS(9_1),
UIApplicationShortcutIconTypeTask NS_ENUM_AVAILABLE_IOS(9_1),
UIApplicationShortcutIconTypeTaskCompleted NS_ENUM_AVAILABLE_IOS(9_1),
UIApplicationShortcutIconTypeAlarm NS_ENUM_AVAILABLE_IOS(9_1),
UIApplicationShortcutIconTypeBookmark NS_ENUM_AVAILABLE_IOS(9_1),
UIApplicationShortcutIconTypeShuffle NS_ENUM_AVAILABLE_IOS(9_1),
UIApplicationShortcutIconTypeAudio NS_ENUM_AVAILABLE_IOS(9_1),
UIApplicationShortcutIconTypeUpdate NS_ENUM_AVAILABLE_IOS(9_1)
} NS_ENUM_AVAILABLE_IOS(9_0);
plist文件添加完成之后需要在AppDelegate.m中添加点击响应事件

- (void)application:(UIApplication *)application performActionForShortcutItem:(UIApplicationShortcutItem *)shortcutItem completionHandler:(void(^)(BOOL succeeded))completionHandler{
    //判断先前我们设置的唯一标识
    if([shortcutItem.type isEqualToString:@"btn1"]){
        FirstViewController *first = [[FirstViewController alloc] init];
        //设置当前的VC 为rootVC
        [self.window.rootViewController presentViewController:first animated:YES completion:^{
        }];
    }
}

代码添加
didFinishLaunchingWithOptions方法中通过如下代码添加:

UIApplicationShortcutIcon *icon = [UIApplicationShortcutIcon iconWithType:UIApplicationShortcutIconTypeLocation];
    UIApplicationShortcutItem *item = [[UIApplicationShortcutItem alloc] initWithType:@"locaction" localizedTitle:@"跳转" localizedSubtitle:@"点击跳转" icon:icon userInfo:nil];
    NSArray *shortItems = @[item];
    [[UIApplication sharedApplication] setShortcutItems:shortItems];

响应事件与静态添加相同的方法与操作。

页面中的的效果添加

首先要使用到3D Touch 效果的页面要遵循UIViewControllerPreviewingDelegate协议,并且使用[self registerForPreviewingWithDelegate:self sourceView:self.view];进行注册。
然后可以通过UIViewControllerPreviewingDelegate的两个方法实现效果。

- (nullable UIViewController *)previewingContext:(id <UIViewControllerPreviewing>)previewingContext viewControllerForLocation:(CGPoint)location 
- (void)previewingContext:(id <UIViewControllerPreviewing>)previewingContext commitViewController:(UIViewController *)viewControllerToCommit

第二个方法是在第一个方法的基础上继续重压的效果实现方法。
然后在相应的效果页面使用-(NSArray<id<UIPreviewActionItem>> *)previewActionItems方法就可实现上拉出现的菜单选项并进行相应操作。
项目代码:https://github.com/mrzsq/TouchDemo

相关文章

网友评论

      本文标题:3D Touch

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