Windows 8.1應用開發之觸控操作

來源:互聯網
上載者:User

與WPF相同Windows 8.1應用中也具有進階觸控操作(Manipulation),其中包含了三種常見的觸屏手勢:平移、縮放、旋轉,通過以下四種事件可為控制項實現各種觸控操作:ManipulationStarting、ManipulationStarted、ManipulationDelta、ManipulationInertiaStarting、ManipulationCompleted。

開啟Visual Studio 2013 Preview,建立Windows Store應用。在XAML代碼中添加Image控制項,將ManipulationMode設定為ALL(也可按需要選擇不同模式),並為其添加ManipulationStarting、ManipulationDelta、ManipulationCompleted事件,以便後續實現相關手勢操作內容。RenderTransform中的CompositeTransform是一個控制項變形組合,可容納多種變形屬性,如平移、旋轉、縮放。

<Grid Background="{StaticResource ApplicationPageBackgroundThemeBrush}">        <Canvas>    <Image x:Name="imageElement" Source="images/cliff.jpg"        Height="460" Width="758" Canvas.Left="300" Canvas.Top="150"        ManipulationMode="All"        ManipulationStarting="image_ManipulationStarting"        ManipulationDelta="image_ManipulationDelta"        ManipulationCompleted="image_ManipulationCompleted">      <Image.RenderTransform>        <CompositeTransform x:Name="imageCT" />      </Image.RenderTransform>    </Image>  </Canvas></Grid>

查看本欄目更多精彩內容:http://www.bianceng.cnhttp://www.bianceng.cn/Programming/net/

接下來,編寫每個事件的具體內容,如下代碼:

private void image_ManipulationStarting(object sender, ManipulationStartingRoutedEventArgs e){  e.Handled = true;}    private void image_ManipulationDelta(object sender, ManipulationDeltaRoutedEventArgs e){  FrameworkElement element = e.OriginalSource as FrameworkElement;  element.Opacity = 0.5;  imageCT.TranslateX += e.Delta.Translation.X;  imageCT.TranslateY += e.Delta.Translation.Y;  imageCT.ScaleX *= e.Delta.Scale;  imageCT.ScaleY *= e.Delta.Scale;  imageCT.Rotation += e.Delta.Rotation;}    private void image_ManipulationCompleted(object sender, ManipulationCompletedRoutedEventArgs e){  FrameworkElement element = e.OriginalSource as FrameworkElement;  element.Opacity = 1;}

這些代碼很好理解,當ManipulationDelta觸發後,首先將控制項透明度設定為0.5,然後捕捉觸控操作並對TranslateX、TranslateY(平移);ScaleX、ScaleY(縮放)、Rotation(旋轉)進行修改。最後ManipulationCompleted結束後將控制項透明度恢複即可。按下F5鍵看看效果如何。

作者:李敬然(Gnie)

出處:{GnieTech} (http://www.cnblogs.com/gnielee/)

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.