MasterofProject

Discussion on the application of Touch 3D in Xamarin.iOS

label PopIOSapplicationIPhonedebugging
2452 people read comment(0) Collection report
Classification:

The new iPhone6s and Plus iPhone6s added a new Touch 3D function, this article briefly describes how to use this function on the Xamarin.iOS.


Touch 3D will not only perceive the user to press the screen, but also the perception of the size of the pressure. Now the IOS simulator does not support 3D touch, we must to test and debug on the iPhone 6s/6s plus real machine should pay attention to a point.

Touch 3D can give you the application to bring a new way of interaction.

  • Sensitivity Pressure - applications can sense the pressure of the user to press the screen. Such painting applications can be changed according to the size of the pressure strokes.
  • And Pop Peek - applications can be used in a single page to get more content. User push the screen will pop up current to additional information, such as some preview information, this behavior is called peek, when the user then forced a point, can jump to page preview information, this behavior is called pop.
  • Actions Quick - this behavior is a bit similar to the right menu in windows, but only for the application icon, showing some of the additional options for this application
Here we are apart.

Sensitivity Pressure



In iOS Xamarin, the size of the pressure is very simple, we can use some of the attributes in the UITouch class to complete. We only need to capture this information in the event of ToucheMoved, please refer to the code
Override void TouchesMoved public (touches NSSet, EVT UIEvent)
{
Base.TouchesMoved (touches, EVT);
Touch UITouch = as UITouch touches.AnyObject;
If (touch! = null)
{
The pressure / / Get
Var force = touch.Force; / / get the pressure
Var maxForce = touch.MaximumPossibleForce; / / get the maximum pressure

Do something with the touch and / the pressure
...
}
}

To note here is that the user presses will trigger TouchesMoved events in such X / y value is constant, if your application before the code is through this event to determine the X, Y values change, need to pay attention to now, X / y must not change. Related documents, please refer toUsing UITouch TouchCanvas: And effectively efficientlyandClass Reference UITouch.

And Pop Peek



This interaction will allow users to access information more quickly, for example, the user browsing a form, users can press table a, get some on the profile information (this behavior is called PEEK), and then forced some, entering the details page (called pop or Pop-ping the behavior of the).

Test equipment to support Touch 3D

Can pass the following code, in the UIViewController to determine whether the current device supports Touch 3D
Override void ViewDidLoad public ()
{
Base.ViewDidLoad ();

Whether to support the 3D Touch / / detection
If (TraitCollection.ForceTouchCapability = = UIForceTouchCapability.Available) {
/ / equipment support
...
}
}


Response to Peek and Pop behavior

We can respond to the behavior of Peek and Pop by inheriting the UIViewControllerPreviewingDelegate class. Please refer to the following code, if the table we mentioned before is called MasterViewController
System using;
System.Collections.Generic using;
UIKit using;
Foundation using;
CoreGraphics using;

DTouch namespace
{
Class PreviewingDelegate public: UIViewControllerPreviewingDelegate
{
Computed Properties #region
MasterViewController MasterController public {set; get;}
#endregion

Constructors #region
公共previewingdelegate(masterviewcontroller mastercontroller)
{
//初始化
this.mastercontroller = mastercontroller;
}

公共previewingdelegate(nsobjectflag:基地(T)T)
{
}

公共previewingdelegate(IntPtr处理):基地(手柄)
{
}
# endregion

#区域覆盖方法
/ / /继续按压触发pop事件
public void(iuiviewcontrollerpreviewing覆盖commitviewcontroller previewingcontext,UIViewController viewcontrollertocommit)
{
//直接使用之前创建好的详情页面
mastercontroller.showviewcontroller(viewcontrollertocommit本);
}

/ / /创建预览页面,当用户触发peek事件
公共重写UIViewController getviewcontrollerforpreview(iuiviewcontrollerpreviewing previewingcontext,cgpoint位置)
{
//判断表格中的条目
VaR indexpath = mastercontroller.tableview.indexpathforrowatpoint(位置);
细胞mastercontroller.tableview.cellat var =(indexpath);
项目mastercontroller.datasource.objects indexpath.row VaR = [ ];

//创建viewcontroller,并设置初始位置
detailviewcontroller mastercontroller.storyboard.instantiateviewcontroller(var =“detailviewcontroller”)为detailviewcontroller;
detailviewcontroller.preferredcontentsize =新的cgsize(0,0);

//填入数据
detailviewcontroller.setdetailitem(项);
detailviewcontroller.navigationitem.leftbarbuttonitem = mastercontroller.splitviewcontroller.displaymodebuttonitem;
detailviewcontroller.navigationitem.leftitemssupplementbackbutton = true;

//设置预览页面的位置,模糊其他页面
细胞骨架;previewingcontext.sourcerect =。

detailviewcontroller回报;
}
# endregion
}
}

代码中getviewcontrollerforpreview函数用来响应peek行为,在这个函数中,首先我们获取当前表单,然后我们加载detailviewcontroller,接着通过preferredcontentsize设置peek窗口的默认大小,最后我们通过previewingcontext.sourcerect =细胞帧这段代码来模糊其他表单,然后返回我们想要的窗口。
commitviewcontroller这个函数会利用我们在peek行为中创建的窗口,来给pop显示。

注册peek和pop行为

在使用peek和pop之前,我们要注册他们,在当前的viewcontroller,请参考下面代码
公共重写viewdidload(void)
{
base.viewdidload();

//判断设备是否支持3d触摸
如果(traitcollection.forcetouchcapability uiforcetouchcapability.available { = =)
//注册pop和peek
registerforpreviewingwithdelegate(新previewingdelegate(本),视图);
}
……

}

在这里,我们调用registerforpreviewingwithdelegate方法把创建的previewingdelegate实例传进去,更多信息请参考

快速行动

使用这个功能,可以让用户从应用的图标上,更快捷和直接地调用应用内的函数,我们可以把这个功能理解为桌面程序的右键菜单



定义静态quick行动

快速actions的行为可以是一个或者多个,我们需要在info.plist中声明他们,代码如下
uiapplicationshortcutitems<>></密钥密钥
> <数组
<,>
uiapplicationshortcutitemicontype<>></密钥密钥
<>></uiapplicationshortcuticontypesearch字符串的字符串
uiapplicationshortcutitemsubtitle<>></密钥密钥
搜索字符串> <想在item字符串> <
uiapplicationshortcutitemtitle<>></密钥密钥
<>></字符串字符串搜索
uiapplicationshortcutitemtype<>></密钥密钥
<>></com.company.appname.000字符串的字符串
<,>
<,>
<><>uiapplicationshortcutitemicontype /关键的关键
<>></uiapplicationshortcuticontypeshare字符串字符串
<><>uiapplicationshortcutitemsubtitle /关键的关键
将字符串> <字符串> < /股an item
<><>uiapplicationshortcutitemtitle /关键的关键
<>></股弦弦
<><>uiapplicationshortcutitemtype /关键的关键
<>></com.company.appname.001字符串字符串
字典></
></阵列

下面是quick actions可以定义的几个键
  • uiapplicationshortcutitemicontype这个键值用于quick actions的图标,可以是以下这几个值
    • uiapplicationshortcuticontypecompose
    • uiapplicationshortcuticontypeplay
    • uiapplicationshortcuticontypepause
    • uiapplicationshortcuticontypeadd
    • uiapplicationshortcuticontypelocation
    • uiapplicationshortcuticontypesearch
    • uiapplicationshortcuticontypeshare
  • uiapplicationshortcutitemsubtitle可以定义副标题
  • uiapplicationshortcutitemtitle定义标题
  • uiapplicationshortcutitemtype这个值会被代码中引用

定义quick action的条目

我们可以通过info.plist定义quick action,然后通过uiapplicationshortcutitemtype定义接入点,来确定用户点击了哪个条目,并与之交互
为了更方便的使用这些接入点,我们也在代码中定义,如下
利用系统;

appsearch命名空间
{
公共静态类shortcutidentifier
{
公共常量字符串=“第一com.company.appname.000”;
公共常量字符串秒=“com.company.appname.001”;
公共常量字符串=“三com.company.appname.002”;
公共常量字符串=“四com.company.appname.003”;
}
}

处理quick action事件

接下来,我们要修改appdelegate.cs中的代码,去响应用户选择的quick action事件,代码如下
利用系统;
……

公共uiapplicationshortcutitem launchedshortcutitem { - };集合;

公共布尔handleshortcutitem(uiapplicationshortcutitem shortcutitem){
handled VAR=false;

   
if(shortcutitem ==空)返回false;

根据shortcut的定义来执行相应函数/ /
开关(shortcutitem.type){
shortcutidentifier第一:房子。
(“第一console.writeline shortcut selected”);
handled = true;
打破;
第二:shortcutidentifier房屋。
console.writeline(“second shortcut selected”);
handled = true;
打破;
shortcutidentifier三:房子。
(三console.writeline shortcut selected”);
handled = true;
打破;
四:shortcutidentifier房屋。
console.writeline(“shortcut selected四”);
handled = true;
打破;
}

handled回车;
}

公共布尔(uiapplication finishedlaunching覆盖应用,nsdictionary launchoptions)
{
shouldperformadditionaldelegatehandling var = true;

获取shortcut条目/ /
launchoptions if(!=null){
launchedshortcutitem = [ ] uiapplicationshortcutitem launchoptions uiapplication.launchoptionsshortcutitemkey As;
shouldperformadditionaldelegatehandling =(launchedshortcutitem==null);
}

shouldperformadditionaldelegatehandling回车;
}

公共重写无效onactivated(uiapplication应用)
{
被选中/处理shortcut
handleshortcutitem(launchedshortcutitem);

设置为空/ /
launchedshortcutitem = null;
}

公共无效performactionforshortcutitem(uiapplication覆盖应用,uiapplicationshortcutitem shortcutitem,uioperationhandler completionhandler)
{
completionhandler(handleshortcutitem(shortcutitem));
}

首先我们定义launchedshortcutitem属性来记录用户选择的quick接着我们重载finishedlaunching方法,通过launchoptions参数来获取是否用户选择了quick,行动,行动。
我们重载onactivated函数,在这里,我们可以处理用户的quick action事件,上面代码中,我们只是打印消息但在实际的应用中我们可以在这里加入响应的代码.在处理quick action之后我们设置launchedshortcutitem属性为空,,,。
Finally, if your application has been in operation, if the user to trigger quick action, the time function PerformActionForShortcutItem will response is, similarly, we also to override this function, quick action, and event handling.

Create dynamic Action Quick entry

In addition to creating a static Action Quick entry in the Info.plist, we can add the Action Quick entry through the code dynamically, we just need to modify the FinishedLaunching, the code is as follows
Override bool FinishedLaunching public (application UIApplication, launchOptions NSDictionary)
{
ShouldPerformAdditionalDelegateHandling var = true;

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

/ / add dynamic entry
If (application.ShortcutItems.Length = = 0) {
Shortcut3 var = UIMutableApplicationShortcutItem new (ShortcutIdentifier.Third, "Play") {
LocalizedSubtitle = "play an item Will",
Icon = UIApplicationShortcutIcon.FromType (UIApplicationShortcutIconType.Play)
};

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

The application of dynamic update / entry
Application.ShortcutItems = UIApplicationShortcutItem[]{shortcut3 new, shortcut4};
}

ShouldPerformAdditionalDelegateHandling return;
}

First of all, we want to determine whether the application has added a dynamic Action Quick entry, if not, we will create two, to the ShortcutItems attribute.
The rest of the code to deal with Action Quick to keep up with a section to add a static Action Quick.
We can both add dynamic and static. More information can refer to

IOS 9 Sample ViewControllerPreview,ApplicationShortcuts: UIApplicationShortcutItem Using, Class Reference UIApplicationShortcutItem, Class Reference UIMutableApplicationShortcutItemandUIApplicationShortcutIcon Reference Class.


summary

This article describes the application of Touch API Xamarin.iOS in 3D, including Sensitivity Peek, and Pop Quick, Actions. Pressure hope that we can create a more innovative applications based on these interactions


This article reprinted from: CSDN Xamarin Jesse Jiang invited technical experts (blogHttp://prog3.com/sbdm/blog/sinat_31967441/article/details/49093073)


top
Three
step on
One
Guess you're looking for
View comments
* the above user comments only represent their personal views, does not represent the views or position of the CSDN website
    personal data
    • visit75493 times
    • Integral:Nine hundred and forty-five
    • Grade
    • Rank:Thousands of miles away
    • original8
    • Reproduced:29
    • Translation:8
    • Comments:31
    My lessons
    Latest comments