美文网首页iOS开发
iOS开发/两种简单实现3dTouch的方法

iOS开发/两种简单实现3dTouch的方法

作者: Grabin | 来源:发表于2017-12-06 17:52 被阅读45次
iPhone用力长按触发3D-Touch效果,如下:
IMG_0377.PNG
实现3D-Touch效果方法有两种:
一、在.plist文件中添加 UIApplicationShortcutItems (不太推荐)
方法a:

直接添加 UIApplicationShortcutItems 项;
再添加item项:(需要的属性)
UIApplicationShortcutItemIconType、
UIApplicationShortcutItemTitle、
UIApplicationShortcutItemType;
这样就已经可以用力长按触发3d-touch了。


image.png
方法b:

右键.plist文件,open as Source Code


image.png

添加以下代码:

<key>UIApplicationShortcutItems</key>
    <array>
        <dict>
            <key>UIApplicationShortcutItemIconType</key>
            <string>UIApplicationShortcutIconTypeShare</string>
            <key>UIApplicationShortcutItemTitle</key>
            <string>分享</string>
            <key>UIApplicationShortcutItemType</key>
            <string>3dTouchDemo.openShare</string>
            <key>UIApplicationShortcutItemUserInfo</key>
            <dict>
                <key>key2</key>
                <string>value2</string>
            </dict>
        </dict>
        <dict>
            <key>UIApplicationShortcutItemIconType</key>
            <string>UIApplicationShortcutIconTypeSearch</string>
            <key>UIApplicationShortcutItemTitle</key>
            <string>搜索</string>
            <key>UIApplicationShortcutItemType</key>
            <string>3dTouchDemo.openSearch</string>
            <key>UIApplicationShortcutItemUserInfo</key>
            <dict>
                <key>key1</key>
                <string>value1</string>
            </dict>
        </dict>
    </array>
二、在AppDelegate中添加 UIApplicationShortcutItems (推荐,维护起来比较方便)
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    
    NSMutableArray *shortcutItemsArray = (NSMutableArray *)[UIApplication sharedApplication].shortcutItems;
    
    UIApplicationShortcutItem *shortItem1 = [[UIApplicationShortcutItem alloc] initWithType:@"3dTouchDemo.openMail" localizedTitle:@"联系我们" localizedSubtitle:nil icon:[UIApplicationShortcutIcon iconWithType:UIApplicationShortcutIconTypeMail] userInfo:nil];
    [shortcutItemsArray addObject:shortItem1];
    
    UIApplicationShortcutItem *shortItem2 = [[UIApplicationShortcutItem alloc] initWithType:@"3dTouchDemo.openLove" localizedTitle:@"收藏" localizedSubtitle:@"" icon:[UIApplicationShortcutIcon iconWithType:UIApplicationShortcutIconTypeLove] userInfo:nil];
    [shortcutItemsArray addObject:shortItem2];
    
    [UIApplication sharedApplication].shortcutItems = shortcutItemsArray;
    
    return YES;
}

// 点击触发此方法
- (void)application:(UIApplication *)application performActionForShortcutItem:(UIApplicationShortcutItem *)shortcutItem completionHandler:(void (^)(BOOL))completionHandler
{
    NSLog(@"%@",shortcutItem.localizedTitle);
    NSLog(@"%@",shortcutItem.type);
}
⚠️注意:如果添加items,运行之后,没有效果,或者出现items错乱,还有其他神奇的情况... 卸载了app,再跑一次.. 会发现,正常了.. 神奇的Xcode。

相关文章

网友评论

    本文标题:iOS开发/两种简单实现3dTouch的方法

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