iOS9 3D Touch iOS 教程 ShortcutItem使用

来源:互联网 发布:淘宝买到假货退款三倍 编辑:程序博客网 时间:2024/04/18 22:36

ShortcutItem


ShortcutItem

静态方式

  • 打开Info.plist文件
  • 在对应UIApplicationShortcutItems关键字下添加item

动态方式

修改当前应用程序的某个shortcutItem

<code class="language-Objctive-C hljs objectivec has-numbering" style="display: block; padding: 0px; background-color: transparent; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-top-left-radius: 0px; border-top-right-radius: 0px; border-bottom-right-radius: 0px; border-bottom-left-radius: 0px; word-wrap: normal; background-position: initial initial; background-repeat: initial initial;">  <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//获取第0个shortcutItem  </span>  <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">id</span> oldItem = [existingShortcutItems objectAtIndex: <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0</span>];    <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//将旧的shortcutItem改变为可修改类型shortcutItem  </span>  <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">id</span> mutableItem = [oldItem mutableCopy];    <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//修改shortcutItem的显示标题  </span>  [mutableItem setLocalizedTitle: @<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"Click Lewis"</span>];</code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; background-color: rgb(238, 238, 238); top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right;"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li><li style="box-sizing: border-box; padding: 0px 5px;">5</li><li style="box-sizing: border-box; padding: 0px 5px;">6</li></ul>

获取当前应用程序的shortcutItems

<code class="language-Objctive-C hljs objectivec has-numbering" style="display: block; padding: 0px; background-color: transparent; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-top-left-radius: 0px; border-top-right-radius: 0px; border-bottom-right-radius: 0px; border-bottom-left-radius: 0px; word-wrap: normal; background-position: initial initial; background-repeat: initial initial;">  <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//获取当前应用程序对象  </span>  <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">UIApplication</span> *app = [<span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">UIApplication</span> sharedApplication];    <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//获取一个应用程序对象的shortcutItem列表  </span>  <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">id</span> existingShortcutItems = [app shortcutItems];</code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; background-color: rgb(238, 238, 238); top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right;"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li></ul>

重置当前应用程序的shortcutItems

<code class="language-Objctive-C hljs objectivec has-numbering" style="display: block; padding: 0px; background-color: transparent; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-top-left-radius: 0px; border-top-right-radius: 0px; border-bottom-right-radius: 0px; border-bottom-left-radius: 0px; word-wrap: normal; background-position: initial initial; background-repeat: initial initial;">  <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//根据旧的shortcutItems生成可变shortcutItems数组  </span>  <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">id</span> updatedShortcutItems = [existingShortcutItems mutableCopy];    <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//修改可变shortcutItems数组中对应index下的元素为新的shortcutItem  </span>  [updatedShortcutItems replaceObjectAtIndex: <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0</span> withObject: mutableItem];    <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//修改应用程序对象的shortcutItems为新的数组  </span>  [app setShortcutItems: updatedShortcutItems];</code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; background-color: rgb(238, 238, 238); top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right;"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li><li style="box-sizing: border-box; padding: 0px 5px;">5</li><li style="box-sizing: border-box; padding: 0px 5px;">6</li></ul>

创建一个新的UIApplicationShortcutItem

  • 初始化函数

    • -initWithType:localizedTitle:localizedSubtitle:icon:userInfo:
    • -initWithType:localizedTitle:
  • 属性

    • localizedTitle:NSString
    • -localizedSubtitle:NSString
    • -type:NSString
    • -icon:UIApplicationShortcutIcon
    • -userInfo:NSDictionary
    • 只有只读特性,想要进行修改时,需要通过mutableCopy方法转变为 
      NSMutableApplicationShortcutItem

创建一个新的Item图标

  • 初始化函数 
    • +iconWithType:
    • +iconWithTemplateImageName:
    • +iconWithContact:

当程序启动时

  • 判断launchOptions字典内的UIApplicationLaunchOptionsShortcutItemKey是否为空
  • 当不为空时,application:didFinishLaunchWithOptions方法返回false,否则返回true
  • 在application:performActionForShortcutItem:completionHandler方法内处理点击事件
0 0