Pan Container 實現(xiàn)簡單拖拽動畫,防止收拾沖突

參考xamarin官方文檔 : https://developer.xamarin.com/guides/xamarin-forms/application-fundamentals/gestures/pan/

項目小需求:仿照ios原點實現(xiàn)在屏幕隨意拖拽和點擊

完成過程中發(fā)現(xiàn)拖拽動畫和點擊時間會沖突(安卓)

提煉官方文檔代碼 :

首先新建 blankpage 集成 ContentPage , 在構(gòu)造函數(shù)中布局 :

```

Content = new AbsoluteLayout

? ? ? ? ? ? {

? ? ? ? ? ? ? ? Padding = new Thickness(20),

? ? ? ? ? ? ? ? Children =

? ? ? ? ? ? ? ? {

? ? ? ? ? ? ? ? ? ? new PanContainer

? ? ? ? ? ? ? ? ? ? {

? ? ? ? ? ? ? ? ? ? ? ? Content = new Image {

? ? ? ? ? ? ? ? ? ? ? ? Source = ImageSource.FromFile ("icon.jpg"),

? ? ? ? ? ? ? ? ? ? ? ? WidthRequest = 1024,

? ? ? ? ? ? ? ? ? ? ? ? HeightRequest = 768

? ? ? ? ? ? ? ? ? ? ? ? }

? ? ? ? ? ? ? ? ? ? }

? ? ? ? ? ? ? ? }

? ? ? ? ? ? };

```

自定義拖動view , 主要自定義拖動的范圍和動畫的狀態(tài)

```

public class PanContainer : ContentView

? ? {

? ? ? ? double x, y;

? ? ? ? public PanContainer()

? ? ? ? {

? ? ? ? ? ? // Set PanGestureRecognizer.TouchPoints to control the

? ? ? ? ? ? // number of touch points needed to pan

? ? ? ? ? ? var panGesture = new PanGestureRecognizer();

? ? ? ? ? ? panGesture.PanUpdated += OnPanUpdated;

? ? ? ? ? ? GestureRecognizers.Add(panGesture);

? ? ? ? }

? ? ? ? private void TapedEventHander(object sender, EventArgs e)

? ? ? ? {

? ? ? ? }

? ? ? ? void OnPanUpdated(object sender, PanUpdatedEventArgs e)

? ? ? ? {

? ? ? ? ? ? switch (e.StatusType)

? ? ? ? ? ? {

? ? ? ? ? ? ? ? case GestureStatus.Running:

? ? ? ? ? ? ? ? ? ? // Translate and ensure we don't pan beyond the wrapped user interface element bounds.

? ? ? ? ? ? ? ? ? ? Content.TranslationX =

? ? ? ? ? ? ? ? ? ? ? Math.Max(Math.Min(0, x + e.TotalX), -Math.Abs(Content.Width - App.ScreenWidth));

? ? ? ? ? ? ? ? ? ? Content.TranslationY =

? ? ? ? ? ? ? ? ? ? ? Math.Max(Math.Min(0, y + e.TotalY), -Math.Abs(Content.Height - App.ScreenHeight));

? ? ? ? ? ? ? ? ? ? break;

? ? ? ? ? ? ? ? case GestureStatus.Completed:

? ? ? ? ? ? ? ? ? ? // Store the translation applied during the pan

? ? ? ? ? ? ? ? ? ? x = Content.TranslationX;

? ? ? ? ? ? ? ? ? ? y = Content.TranslationY;

? ? ? ? ? ? ? ? ? ? break;

? ? ? ? ? ? }

? ? ? ? }

? ? }

```

以上代碼便可以實現(xiàn)一個簡單的拖拽動畫

在OnPanUpdated這個事件里面 , 分別有4種狀態(tài) , Started , Running , Completed , Canceled

Started : 手指按下去

Running : 手指移動

Completed : 收拾抬起來

Canceled : 取消 , 但是測試狀態(tài)未測到這個狀態(tài)

輸出日志 :

```

--------start-----------03-13 01:02:38.537

--------move-----------03-13 01:02:38.553

--------move-----------03-13 01:02:38.662

--------move-----------03-13 01:02:38.692

--------end-----------03-13 01:02:44.180

```

根據(jù)上面的輸出結(jié)果 , 我們可以根據(jù)開始時間和結(jié)束的時間差來解決手勢沖突的問題

在手勢開始的狀態(tài)記錄手指點下去的時間 , 結(jié)束時計算時間差

```

case GestureStatus.Started:

? ? ? ? ? ? ? ? ? ? StartTime = DateTime.Now;

case GestureStatus.Completed:

? ? ? ? ? ? ? ? ? ? offsetX = this.TranslationX;

? ? ? ? ? ? ? ? ? ? offsetY = this.TranslationY;

? ? ? ? ? ? ? ? ? ? TimeSpan timespan = DateTime.Now - StartTime;

? ? ? ? ? ? ? ? ? ? MoveTimeSpan = timespan.Milliseconds + timespan.Seconds * 1000;

```

之后我們需要在使用的時候?qū)崿F(xiàn)點擊事件 :

```

private void TapedEventHander(object sender, EventArgs e)

? ? ? ? {

? ? ? ? ? ? var touchPanContainere = sender as PanContainer ;

? ? ? ? ? ? if (touchPanContainere.MoveTimeSpan < 500)

? ? ? ? ? ? {

? ? ? ? ? ? ? ? ? ? todo點擊事件

? ? ? ? ? ? }

? ? ? ? ? ? touchPanContainere.MoveTimeSpan = 0;

? ? ? ? }

```


同時要考慮手勢的范圍 , 防止拖拽屏幕之外 , 我們要在移動GestureStatus.Running的時候限制動畫的偏移量:

case GestureStatus.Running:


? ? ? ? ? ? ? ? ? ? this.Content.TranslationX = e.TotalX;

? ? ? ? ? ? ? ? ? ? if (e.TotalX+ offsetX <= -(App.Current.MainPage.Width-Width))

? ? ? ? ? ? ? ? ? ? {

? ? ? ? ? ? ? ? ? ? ? ? this.Content.TranslationX = -(App.Current.MainPage.Width - Width + offsetX);

? ? ? ? ? ? ? ? ? ? }

? ? ? ? ? ? ? ? ? ? if (e.TotalX+ offsetX >= 0)

? ? ? ? ? ? ? ? ? ? {

? ? ? ? ? ? ? ? ? ? ? ? this.Content.TranslationX = - offsetX;


? ? ? ? ? ? ? ? ? ? }


? ? ? ? ? ? ? ? ? ? this.Content.TranslationY = e.TotalY;

? ? ? ? ? ? ? ? ? ? if (e.TotalY + offsetY <= -(App.Current.MainPage.Height - Height - 140))

? ? ? ? ? ? ? ? ? ? {

? ? ? ? ? ? ? ? ? ? ? ? this.Content.TranslationY = -(App.Current.MainPage.Height - Height - 140 + offsetY);

? ? ? ? ? ? ? ? ? ? }

? ? ? ? ? ? ? ? ? ? if (e.TotalY + offsetY >= 0)

? ? ? ? ? ? ? ? ? ? {

? ? ? ? ? ? ? ? ? ? ? ? this.Content.TranslationY = -offsetY;

? ? ? ? ? ? ? ? ? ? }


? ? ? ? ? ? ? ? ? ? break;

? ? ? ? ? ? ? ? case GestureStatus.Completed:


? ? ? ? ? ? ? ? ? ? offsetX += Content.TranslationX;

? ? ? ? ? ? ? ? ? ? offsetY += Content.TranslationY;

? ? ? ? ? ? ? ? ? ? Content.TranslationX = 0;

? ? ? ? ? ? ? ? ? ? Content.TranslationY = 0;

? ? ? ? ? ? ? ? ? ? this.TranslationX = offsetX;

? ? ? ? ? ? ? ? ? ? this.TranslationY = offsetY;

? ? ? ? ? ? ? ? ? ? TimeSpan timespan = DateTime.Now - StartTime;

? ? ? ? ? ? ? ? ? ? MoveTimeSpan = timespan.Milliseconds + timespan.Seconds * 1000;

? ? ? ? ? ? ? ? ? ? break;

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末杈绸,一起剝皮案震驚了整個濱河市,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌序臂,老刑警劉巖,帶你破解...
    沈念sama閱讀 218,451評論 6 506
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件派哲,死亡現(xiàn)場離奇詭異螺男,居然都是意外死亡,警方通過查閱死者的電腦和手機檩互,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,172評論 3 394
  • 文/潘曉璐 我一進店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來咨演,“玉大人闸昨,你說我怎么就攤上這事。” “怎么了饵较?”我有些...
    開封第一講書人閱讀 164,782評論 0 354
  • 文/不壞的土叔 我叫張陵拍嵌,是天一觀的道長。 經(jīng)常有香客問我循诉,道長横辆,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 58,709評論 1 294
  • 正文 為了忘掉前任茄猫,我火速辦了婚禮狈蚤,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘划纽。我一直安慰自己脆侮,他們只是感情好,可當我...
    茶點故事閱讀 67,733評論 6 392
  • 文/花漫 我一把揭開白布勇劣。 她就那樣靜靜地躺著靖避,像睡著了一般。 火紅的嫁衣襯著肌膚如雪比默。 梳的紋絲不亂的頭發(fā)上幻捏,一...
    開封第一講書人閱讀 51,578評論 1 305
  • 那天,我揣著相機與錄音命咐,去河邊找鬼篡九。 笑死,一個胖子當著我的面吹牛侈百,可吹牛的內(nèi)容都是我干的瓮下。 我是一名探鬼主播,決...
    沈念sama閱讀 40,320評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼钝域,長吁一口氣:“原來是場噩夢啊……” “哼讽坏!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起例证,我...
    開封第一講書人閱讀 39,241評論 0 276
  • 序言:老撾萬榮一對情侶失蹤路呜,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后织咧,有當?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體胀葱,經(jīng)...
    沈念sama閱讀 45,686評論 1 314
  • 正文 獨居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 37,878評論 3 336
  • 正文 我和宋清朗相戀三年笙蒙,在試婚紗的時候發(fā)現(xiàn)自己被綠了抵屿。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點故事閱讀 39,992評論 1 348
  • 序言:一個原本活蹦亂跳的男人離奇死亡捅位,死狀恐怖轧葛,靈堂內(nèi)的尸體忽然破棺而出搂抒,到底是詐尸還是另有隱情,我是刑警寧澤尿扯,帶...
    沈念sama閱讀 35,715評論 5 346
  • 正文 年R本政府宣布求晶,位于F島的核電站,受9級特大地震影響衷笋,放射性物質(zhì)發(fā)生泄漏芳杏。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點故事閱讀 41,336評論 3 330
  • 文/蒙蒙 一辟宗、第九天 我趴在偏房一處隱蔽的房頂上張望爵赵。 院中可真熱鬧,春花似錦泊脐、人聲如沸亚再。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,912評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至则剃,卻和暖如春耘柱,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背棍现。 一陣腳步聲響...
    開封第一講書人閱讀 33,040評論 1 270
  • 我被黑心中介騙來泰國打工调煎, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留,地道東北人己肮。 一個月前我還...
    沈念sama閱讀 48,173評論 3 370
  • 正文 我出身青樓士袄,卻偏偏與公主長得像,于是被迫代替她去往敵國和親谎僻。 傳聞我的和親對象是個殘疾皇子娄柳,可洞房花燭夜當晚...
    茶點故事閱讀 44,947評論 2 355

推薦閱讀更多精彩內(nèi)容

  • 重新框定問題,改變邏輯種類層次艘绍,理解起來很容易赤拒,難度在于當事人不愿邁出原有的框框,即使旁人能夠清晰支出新的框架诱鞠,當...
    亦周閱讀 483評論 0 49
  • 當我通過學(xué)習(xí)知道“成交一切為了愛”這句話航夺,但我并不知道它的真正含義時蕉朵,這句話只是一句口號。 當我學(xué)會被成交時阳掐,我感...
    宣力亢閱讀 876評論 0 0
  • wxml template css [github上的開源demo][1][1]:https://github.c...
    EnjoyWT閱讀 158評論 0 0
  • 母親天生麗質(zhì)始衅,一輩子不愛化妝冷蚂,不尚華衣美服,不好珍饈美味觅闽,對旅游和交友也不是很熱衷帝雇,她的樂趣一是讀唐詩,二...
    鄧鼎鼎閱讀 485評論 3 3