新的iPhone6s和iPhone6s Plus新添加了3D Touch的功能,本文簡單介紹一下如何在Xamarin.iOS上面使用該功能。
3D Touch不僅會感知用戶按壓螢屏,也會感知壓力的大小。要注意一點的是,現在的iOS模擬器并不支持3D Touch,大家必須要在iPhone 6s/6s Plus 真機上進行測驗和除錯。
3D Touch可以給你的應用帶來全新的互動方式.
?Pressure Sensitivity - 應用可以感知用戶按壓螢屏的壓力。這樣一些繪畫應用可以根據壓力的大小改變筆觸。
?Peek and Pop - 應用可在單一頁面中獲取更多的內容。用戶用力按壓螢屏會彈出當前條目的額外資訊,比如一些預覽資訊,這個行為叫做Peek,當用戶再用力一點,可以跳轉到預覽資訊的頁面,這個行為叫做Pop。
?Quick Actions - 這個行為有點類似于windows中的右鍵選單,但是只是針對于應用圖示的,顯示一些關于這個應用的額外選項
下面我們分開來講
Pressure Sensitivity

在Xamarin iOS中,獲取壓力的大小非常簡單,我們可以通過UITouch類中的一些屬性來完成。我們只需要在ToucheMoved的事件中捕獲這些資訊,請參考一下代碼
public override void TouchesMoved (NSSet touches, UIEvent evt)
{
base.TouchesMoved (touches, evt);
UITouch touch = touches.AnyObject as UITouch;
if (touch != null)
{
// Get the pressure
var force = touch.Force; //獲取壓力
var maxForce = touch.MaximumPossibleForce; //獲取壓力最大值
// Do something with the touch and the pressure
...
}
}
這里要注意的是,用戶按壓會觸發TouchesMoved的事件,在這樣的情況下X/Y的值是不變的,如果你的應用之前的代碼是通過這個事件來判斷X,Y值的是否改變,現在需要注意,X/Y不一定改變。相關檔案請參考 TouchCanvas: Using UITouch efficiently and effectively 和 UITouch Class Reference.
Peek and Pop

這個互動行為會讓用戶更快的獲取資訊,比如用戶在瀏覽一個表格,用戶可以按壓表格中的某項,獲取一些關于該項的概況資訊(這個行為叫做Peek),再用力一些,就可進入該項的詳情頁(這個行為叫做Pop或Pop-ping)。
檢測設備是否支持3D Touch
可以通過下面的代碼,在UIViewController 中判斷當前設備是否支持3D Touch
public override void ViewDidLoad ()
{
base.ViewDidLoad ();
// 檢測是否支持3D Touch
if (TraitCollection.ForceTouchCapability == UIForceTouchCapability.Available) {
// 設備支持
...
}
}
回應Peek和Pop行
我們可以通過繼承UIViewControllerPreviewingDelegate類來回應Peek和Pop的行為。請參考下面代碼,假設我們之前提到的表格叫做MasterViewController
using System;
using System.Collections.Generic;
using UIKit;
using Foundation;
using CoreGraphics;
namespace DTouch
{
public class PreviewingDelegate : UIViewControllerPreviewingDelegate
{
#region Computed Properties
public MasterViewController MasterController { get; set; }
#endregion
#region Constructors
public PreviewingDelegate (MasterViewController masterController)
{
// Initialize
this.MasterController = masterController;
}
public PreviewingDelegate (NSObjectFlag t) : base(t)
{
}
public PreviewingDelegate (IntPtr handle) : base (handle)
{
}
#endregion
#region Override Methods
/// 繼續按壓觸發Pop事件
public override void CommitViewController (IUIViewControllerPreviewing previewingContext, UIViewController viewControllerToCommit)
{
// 直接使用之前創建好的詳情頁面
MasterController.ShowViewController(viewControllerToCommit,this);
}
/// 創建預覽頁面,當用戶觸發Peek事件
public override UIViewController GetViewControllerForPreview (IUIViewControllerPreviewing previewingContext, CGPoint location)
{
// 判斷表格中的條目
var indexPath = MasterController.TableView.IndexPathForRowAtPoint (location);
var cell = MasterController.TableView.CellAt (indexPath);
var item = MasterController.dataSource.Objects [indexPath.Row];
// 創建ViewController,并設定初始位置
var detailViewController = MasterController.Storyboard.InstantiateViewController ("DetailViewController") as DetailViewController;
detailViewController.PreferredContentSize = new CGSize (0, 0);
// 填入資料
detailViewController.SetDetailItem (item);
detailViewController.NavigationItem.LeftBarButtonItem = MasterController.SplitViewController.DisplayModeButtonItem;
detailViewController.NavigationItem.LeftItemsSupplementBackButton = true;
// 設定預覽頁面的位置,模糊其他頁面
previewingContext.SourceRect = cell.Frame;
return detailViewController;
}
#endregion
}
}
代碼中GetViewControllerForPreview函式用來回應Peek行為,在這個函式中,首先我們獲取當前表單,然后我們加載DetailViewController,接著通過PreferredContentSize設定Peek視窗的默認大小,最后我們通過previewingContext.SourceRect = cell.Frame 這段代碼來模糊其他表單,然后回傳我們想要的視窗。
CommitViewController 這個函式會利用我們在Peek行為中創建的視窗,來給Pop顯示。
注冊Peek和Pop行為
在使用Peek和Pop之前,我們要注冊他們,在當前的ViewController,請參考下面代碼
public override void ViewDidLoad ()
{
base.ViewDidLoad ();
// 判斷設備是否支持3D Touch
if (TraitCollection.ForceTouchCapability == UIForceTouchCapability.Available) {
// 注冊Pop和Peek
RegisterForPreviewingWithDelegate(new PreviewingDelegate(this), View);
}
...
}
在這里,我們呼叫RegisterForPreviewingWithDelegate方法把創建的PreviewingDelegate實體傳進去,更多資訊請參考
iOS 9 ApplicationShortcuts Sample , ViewControllerPreviews: Using the UIViewController previewing APIs , UIPreviewAction Class Reference, UIPreviewActionGroup Class Reference 和 UIPreviewActionItem Protocol Reference.
未完,更多技術資訊,請看:[技術分享]淺談3D Touch 在Xamarin.iOS上的應用 (下)
查看更多Xamarin技術文章:http://blog.csdn.net/xamarin?viewmode=contents
了解最新Xamarin特惠方案:http://mall.csdn.net/product/500
uj5u.com熱心網友回復:
看起來很深奧啊,不明覺厲。uj5u.com熱心網友回復:
我買了6s,可是對于很多功能還不會用....
uj5u.com熱心網友回復:
uj5u.com熱心網友回復:
果斷收藏,學習!uj5u.com熱心網友回復:
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/23249.html
標籤:Xamarin技術
下一篇:護理管理系統開發維護
