[技术分享]浅谈3D Touch 在Xamarin.iOS上的应用 (下)

Allisvanity 2015-10-22 09:30:03
[技术分享]浅谈3D Touch 在Xamarin.iOS上的应用 (上)
定义静态Quick Actions
Quick Actions的行为可以是一个或者多个,我们需要在Info.plist中声明他们,代码如下
    <key>UIApplicationShortcutItems</key>  
<array>
<dict>
<key>UIApplicationShortcutItemIconType</key>
<string>UIApplicationShortcutIconTypeSearch</string>
<key>UIApplicationShortcutItemSubtitle</key>
<string>Will search for an item</string>
<key>UIApplicationShortcutItemTitle</key>
<string>Search</string>
<key>UIApplicationShortcutItemType</key>
<string>com.company.appname.000</string>
</dict>
<dict>
<key>UIApplicationShortcutItemIconType</key>
<string>UIApplicationShortcutIconTypeShare</string>
<key>UIApplicationShortcutItemSubtitle</key>
<string>Will share an item</string>
<key>UIApplicationShortcutItemTitle</key>
<string>Share</string>
<key>UIApplicationShortcutItemType</key>
<string>com.company.appname.001</string>
</dict>
</array>

下面是Quick Actions可以定义的几个键

UIApplicationShortcutItemIconType 这个键值用于Quick Actions的图标,可以是以下这几个值
UIApplicationShortcutIconTypeCompose
UIApplicationShortcutIconTypePlay
UIApplicationShortcutIconTypePause
UIApplicationShortcutIconTypeAdd
UIApplicationShortcutIconTypeLocation
UIApplicationShortcutIconTypeSearch
UIApplicationShortcutIconTypeShare
UIApplicationShortcutItemSubtitle 可以定义副标题
UIApplicationShortcutItemTitle 定义标题
UIApplicationShortcutItemType 这个值会被代码中引用

定义Quick Action的条目
我们可以通过Info.plist定义Quick Action,然后通过UIApplicationShortcutItemType定义接入点,来确定用户点击了哪个条目,并与之交互
为了更方便的使用这些接入点,我们也在代码中定义,如下
    using System;  

namespace AppSearch
{
public static class ShortcutIdentifier
{
public const string First = "com.company.appname.000";
public const string Second = "com.company.appname.001";
public const string Third = "com.company.appname.002";
public const string Fourth = "com.company.appname.003";
}
}

处理Quick Action事件
接下来,我们要修改AppDelegate.cs中的代码,去响应用户选择的Quick Action事件,代码如下
    using System;  
...

public UIApplicationShortcutItem LaunchedShortcutItem { get; set; }

public bool HandleShortcutItem(UIApplicationShortcutItem shortcutItem) {
var handled = false;


if (shortcutItem == null) return false;

// 根据Shortcut的定义来执行相应函数
switch (shortcutItem.Type) {
case ShortcutIdentifier.First:
Console.WriteLine ("First shortcut selected");
handled = true;
break;
case ShortcutIdentifier.Second:
Console.WriteLine ("Second shortcut selected");
handled = true;
break;
case ShortcutIdentifier.Third:
Console.WriteLine ("Third shortcut selected");
handled = true;
break;
case ShortcutIdentifier.Fourth:
Console.WriteLine ("Forth shortcut selected");
handled = true;
break;
}

return handled;
}

public override bool FinishedLaunching (UIApplication application, NSDictionary launchOptions)
{
var shouldPerformAdditionalDelegateHandling = true;

// 获取shortcut条目
if (launchOptions != null) {
LaunchedShortcutItem = launchOptions [UIApplication.LaunchOptionsShortcutItemKey] as UIApplicationShortcutItem;
shouldPerformAdditionalDelegateHandling = (LaunchedShortcutItem == null);
}

return shouldPerformAdditionalDelegateHandling;
}

public override void OnActivated (UIApplication application)
{
// 处理shortcut 被选中
HandleShortcutItem(LaunchedShortcutItem);

// 设置为空
LaunchedShortcutItem = null;
}

public override void PerformActionForShortcutItem (UIApplication application, UIApplicationShortcutItem shortcutItem, UIOperationHandler completionHandler)
{
completionHandler(HandleShortcutItem(shortcutItem));
}

首先,我们定义LaunchedShortcutItem属性来记录用户选择的Quick Action,接着我们重载FinishedLaunching方法,通过launchOptions参数来获取是否用户选择了Quick Action。
我们重载OnActivated函数,在这里,我们可以处理用户的Quick Action事件,上面代码中,我们只是打印消息,但在实际的应用中,我们可以在这里加入响应的代码。在处理Quick Action之后,我们设置LaunchedShortcutItem属性为空。
最后,如果你的应用已经在运行,如果用户再触发Quick Action,这个时候PerformActionForShortcutItem函数会被响应,同样,我们也要重载这个函数,处理Quick Action事件。

创建动态Quick Action条目
除了我们在Info.plist中创建静态Quick Action条目以外,我们可以通过代码动态添加Quick Action条目,我们只需修改一下FinishedLaunching,代码如下
    public override bool FinishedLaunching (UIApplication application, NSDictionary launchOptions)  
{
var shouldPerformAdditionalDelegateHandling = true;

if (launchOptions != null) {
LaunchedShortcutItem = launchOptions [UIApplication.LaunchOptionsShortcutItemKey] as UIApplicationShortcutItem;
shouldPerformAdditionalDelegateHandling = (LaunchedShortcutItem == null);
}

// 添加动态条目
if (application.ShortcutItems.Length == 0) {
var shortcut3 = new UIMutableApplicationShortcutItem (ShortcutIdentifier.Third, "Play") {
LocalizedSubtitle = "Will play an item",
Icon = UIApplicationShortcutIcon.FromType(UIApplicationShortcutIconType.Play)
};

var shortcut4 = new UIMutableApplicationShortcutItem (ShortcutIdentifier.Fourth, "Pause") {
LocalizedSubtitle = "Will pause an item",
Icon = UIApplicationShortcutIcon.FromType(UIApplicationShortcutIconType.Pause)
};

// 更新应用中的动态条目
application.ShortcutItems = new UIApplicationShortcutItem[]{shortcut3, shortcut4};
}

return shouldPerformAdditionalDelegateHandling;
}

首先,我们要确定application是否已经添加了动态的Quick Action条目,如果没有,我们就创建两个,给ShortcutItems属性赋值。
剩下处理Quick Action的代码跟上一节添加静态的Quick Action的一样。
我们既可以添加动态的也可以添加静态的。更多信息可以参考

iOS 9 ViewControllerPreview Sample , ApplicationShortcuts: Using UIApplicationShortcutItem , UIApplicationShortcutItem Class Reference, UIMutableApplicationShortcutItem Class ReferenceUIApplicationShortcutIcon Class Reference.


总结
本文介绍了3D Touch API在Xamarin.iOS中的应用,其中包括Pressure Sensitivity, Peek and Pop, Quick Actions. 希望大家能基于这些交互创造出更新颖的应用

查看更多Xamarin技术文章:http://blog.csdn.net/xamarin?viewmode=contents
了解最新Xamarin特惠方案:http://mall.csdn.net/product/500
...全文
1773 回复 打赏 收藏 转发到动态 举报
写回复
用AI写文章
回复
切换为时间正序
请发表友善的回复…
发表回复

436

社区成员

发帖
与我相关
我的任务
社区描述
欢迎来到Xamarin技术交流论坛学习交流,Xamarin是一个行动App开发平台,开发人员透过Xamarin可开发出iOS、Android 与Windows 等平台的...
社区管理员
  • Xamarin技术社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧