2015-11-10.周二,天气晴
3dtouch的实践
首先,这个功能只有在iOS9才会有的,所以首先第一件事就是判断系统版本号,在其他系统版本不执行相关代码,
define IOS_VERSION [[[UIDevice currentDevice] systemVersion] floatValue]
在app delegate里的didFinishLaunchingWithOptions方法里面创建弹出来得几个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;
- }
创建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。