[英]Moving an object according to the direction button in WP8 App
我正在嘗試根據方向按鈕上,左,右,下移動對象。
我將保證金屬性設置為:-
img.Margin = new Thickness(l, t, r, b); //L T R B
我正在根據所需的所需移動來增加/減少這些值。
我可以通過click事件移動對象。 但是,無論何時為用戶按下並按住按鈕,我都想沿所需方向移動對象。 一旦用戶釋放按鈕,運動也應停止。
我嘗試使用hold事件,但是該操作執行一次然后停止。
在另一次嘗試中,我嘗試循環我的語句,但該應用程序停頓了。
請幫我。 謝謝!
編輯:-
我處理了ManipulationStarted,ManipulationDelta,ManipulationCompleted事件。
現在,只要按住按鈕,就可以移動對象。 但是,我面臨的新問題是必須不斷保持手指在屏幕上移動才能執行動作。
向上按鈕(沿垂直方向移動對象的按鈕)的代碼為:-
public double l = 0.0, t = 0.0, r = 0.0, b = 0.0;
public void move()
{
img.Margin = new Thickness(l, t, r, b); //L T R B
}
private void up_ManipulationStarted(object sender, ManipulationStartedEventArgs e)
{
}
private void up_ManipulationDelta(object sender, ManipulationDeltaEventArgs e)
{
t = t + 1.0;
move();
}
private void up_ManipulationCompleted(object sender, ManipulationCompletedEventArgs e)
{
}
我不確定此方法是否正確。 勸告。 謝謝。
您應該使用 ManipulationStarted
和ManipulationCompleted
事件。它們適用於此處所述的“ Tap
並Hold
手勢: https : //msdn.microsoft.com/zh-cn/library/windows/apps/ff426933(v=vs.105).aspx
更新
為了正確檢測敲擊的開始和結束,我建議使用MouseEnter
和MouseLeaving
事件。 這里有一個示例,顯示了我如何向下移動一個對象。 當前這是屏幕中央的正方形:
<Grid x:Name="objectToMove" Background="red" Height="100" Width="100">
<Grid.RenderTransform>
<TranslateTransform x:Name="verticalTransform" Y="0" />
</Grid.RenderTransform>
</Grid>
后面的代碼:
AutoResetEvent autoEvent = new AutoResetEvent(true);
System.Threading.Timer dt;
private void toggle_MouseEnter(object sender, MouseEventArgs e)
{
dt = new System.Threading.Timer(new TimerCallback(MoveFunct), autoEvent, 0, 1);
}
private void MoveFunct(Object stateInfo)
{
Deployment.Current.Dispatcher.BeginInvoke(() => { verticalTransform.Y += 3; });
}
private void toggle_MouseLeave(object sender, MouseEventArgs e)
{
dt.Dispose();
}
請注意, Timer
構造函數的最后一個參數包括刻度之間的間隔。 同樣在MoveFunct
函數內部,我正在調用Dispatcher
方法,否則它將無法訪問UI線程。 我認為在示例中,我使用了TranslateTransform
,它優於Margin
操作,因為它需要更新整個元素的可視樹。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.