iOS開發(fā)/兩種簡單實現(xiàn)3dTouch的方法

iPhone用力長按觸發(fā)3D-Touch效果,如下:
IMG_0377.PNG
實現(xiàn)3D-Touch效果方法有兩種:
一、在.plist文件中添加 UIApplicationShortcutItems (不太推薦)
方法a:

直接添加 UIApplicationShortcutItems 項;
再添加item項:(需要的屬性)
UIApplicationShortcutItemIconType、
UIApplicationShortcutItemTitle、
UIApplicationShortcutItemType;
這樣就已經(jīng)可以用力長按觸發(fā)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 (推薦,維護(hù)起來比較方便)
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    
    NSMutableArray *shortcutItemsArray = (NSMutableArray *)[UIApplication sharedApplication].shortcutItems;
    
    UIApplicationShortcutItem *shortItem1 = [[UIApplicationShortcutItem alloc] initWithType:@"3dTouchDemo.openMail" localizedTitle:@"聯(lián)系我們" 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;
}

// 點擊觸發(fā)此方法
- (void)application:(UIApplication *)application performActionForShortcutItem:(UIApplicationShortcutItem *)shortcutItem completionHandler:(void (^)(BOOL))completionHandler
{
    NSLog(@"%@",shortcutItem.localizedTitle);
    NSLog(@"%@",shortcutItem.type);
}
??注意:如果添加items,運行之后,沒有效果,或者出現(xiàn)items錯亂,還有其他神奇的情況... 卸載了app,再跑一次.. 會發(fā)現(xiàn),正常了.. 神奇的Xcode。
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

推薦閱讀更多精彩內(nèi)容