2015-11-10.周二顿乒,天氣晴
3dtouch的實踐
首先,這個功能只有在iOS9才會有的铭拧,所以首先第一件事就是判斷系統(tǒng)版本號,在其他系統(tǒng)版本不執(zhí)行相關代碼恃锉,
define IOS_VERSION [[[UIDevice currentDevice] systemVersion] floatValue]
在app delegate里的didFinishLaunchingWithOptions方法里面創(chuàng)建彈出來得幾個item搀菩。
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Override point for customization after application launch.
[self createItem];
UIApplicationShortcutItem *item = [launchOptions valueForKey:UIApplicationLaunchOptionsShortcutItemKey];
if (item)
{
NSLog(@"We've launched from shortcut item: %@", item.localizedTitle);
}
else
{
NSLog(@"We've launched properly.");
}
return YES;
- }
創(chuàng)建item可以在plist里寫也可以在代碼里面寫
-(void) createItem
{
//自定義icon 的初始化方法
// UIApplicationShortcutIcon *icon1 = [UIApplicationShortcutIcon iconWithTemplateImageName:@"your_icon"];
// UIMutableApplicationShortcutItem *item0 = [[UIMutableApplicationShortcutItem alloc] initWithType:@"com.your.helloWorld" localizedTitle:@"Title" localizedSubtitle:@"sub Title" icon:icon1 userInfo:nil];
//這種是隨意沒有icon 的
UIMutableApplicationShortcutItem *item1 = [[UIMutableApplicationShortcutItem alloc] initWithType:@"test.com.A" localizedTitle:@"三條A"];
UIMutableApplicationShortcutItem *item2 = [[UIMutableApplicationShortcutItem alloc] initWithType:@"test.com.B" localizedTitle:@"三條B"];
UIMutableApplicationShortcutItem *item3 = [[UIMutableApplicationShortcutItem alloc] initWithType:@"test.com.C" localizedTitle:@"三條C"];
NSArray *addArr = @[item2,item3,item1];
//為什么這兩句話可以不用,因為我們可以在plist 里面 加入 UIApplicationShortcutItems
// NSArray *existArr = [UIApplication sharedApplication].shortcutItems;
// [UIApplication sharedApplication].shortcutItems = [existArr arrayByAddingObjectsFromArray:addArr];
[UIApplication sharedApplication].shortcutItems = addArr;
}
在下面方法里面對操作了彈出item項的動作做處理
- (void)application:(UIApplication *)application performActionForShortcutItem:(UIApplicationShortcutItem *)shortcutItem completionHandler:(void (^)(BOOL))completionHandler {
// react to shortcut item selections
NSLog(@"A shortcut item was pressed. It was %@.", shortcutItem.localizedTitle);
- }
關于UIMutableApplicationShortcutItem 的icon的設置見下面描述破托,35x35的肪跋,
//Icons should be square, single color, and 35x35 points, as shown in these template files and as described in Template Images in UIKit User Interface Catalog and in iOS Human Interface Guidelines.
3D Touch不僅可以在icon上使用,也可以用在view controller中土砂,具體使用例子可以看shanghai love代碼的baseviewcontroller州既。