3Dtouch

2015-11-10.周二,天气晴

3dtouch的实践
首先,这个功能只有在iOS9才会有的,所以首先第一件事就是判断系统版本号,在其他系统版本不执行相关代码,

define IOS_VERSION [[[UIDevice currentDevice] systemVersion] floatValue]

在app delegate里的didFinishLaunchingWithOptions方法里面创建弹出来得几个item。

    • (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
  1. // Override point for customization after application launch.  
    
  2. [self createItem];  
    
  3. UIApplicationShortcutItem *item = [launchOptions valueForKey:UIApplicationLaunchOptionsShortcutItemKey];  
    
  4. if (item)  
    
  5. {  
    
  6.     NSLog(@"We've launched from shortcut item: %@", item.localizedTitle);  
    
  7. }  
    
  8. else  
    
  9. {  
    
  10.     NSLog(@"We've launched properly.");  
    
  11. }  
    
  12. return YES;  
    
  13. }

创建item可以在plist里写也可以在代码里面写

  1. -(void) createItem

  2. {

  3. //自定义icon 的初始化方法  
    
  4. // UIApplicationShortcutIcon *icon1 = [UIApplicationShortcutIcon iconWithTemplateImageName:@"your_icon"];

  5. // UIMutableApplicationShortcutItem *item0 = [[UIMutableApplicationShortcutItem alloc] initWithType:@"com.your.helloWorld" localizedTitle:@"Title" localizedSubtitle:@"sub Title" icon:icon1 userInfo:nil];

  6. //这种是随意没有icon 的  
    
  7. UIMutableApplicationShortcutItem *item1 = [[UIMutableApplicationShortcutItem alloc] initWithType:@"test.com.A" localizedTitle:@"三条A"];  
    
  8. UIMutableApplicationShortcutItem *item2 = [[UIMutableApplicationShortcutItem alloc] initWithType:@"test.com.B" localizedTitle:@"三条B"];  
    
  9. UIMutableApplicationShortcutItem *item3 = [[UIMutableApplicationShortcutItem alloc] initWithType:@"test.com.C" localizedTitle:@"三条C"];  
    
  10. NSArray *addArr = @[item2,item3,item1];  
    
  11. //为什么这两句话可以不用,因为我们可以在plist 里面 加入 UIApplicationShortcutItems  
    
  12. // NSArray *existArr = [UIApplication sharedApplication].shortcutItems;

  13. // [UIApplication sharedApplication].shortcutItems = [existArr arrayByAddingObjectsFromArray:addArr];

  14. [UIApplication sharedApplication].shortcutItems = addArr;  
    
  15. }

在下面方法里面对操作了弹出item项的动作做处理

    • (void)application:(UIApplication *)application performActionForShortcutItem:(UIApplicationShortcutItem *)shortcutItem completionHandler:(void (^)(BOOL))completionHandler {
  1. // react to shortcut item selections  
    
  2. NSLog(@"A shortcut item was pressed. It was %@.", shortcutItem.localizedTitle);  
    
  3. }

关于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。

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容