[C#/MAUI/.NET6] PanGestureRecognizer 클래스 : PanUpdated 이벤트를 사용해 패닝 처리하기 (기능 개선)
■ PanGestureRecognizer 클래스의 PanUpdated 이벤트를 사용해 패닝을 처리하는 방법을 보여준다. (기능 개선) ▶ PanContentView.cs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 |
namespace TestProject; /// <summary> /// 패닝 컨텐트 뷰 /// </summary> public class PanContentView : Frame { //////////////////////////////////////////////////////////////////////////////////////////////////// Field ////////////////////////////////////////////////////////////////////////////////////////// Private #region Field /// <summary> /// 현재 X /// </summary> private double currentX; /// <summary> /// 현재 Y /// </summary> private double currentY; #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Constructor ////////////////////////////////////////////////////////////////////////////////////////// Public #region 생성자 - PanContentView() /// <summary> /// 생성자 /// </summary> public PanContentView() { Padding = new Thickness(0); IsClippedToBounds = true; PanGestureRecognizer recognizer = new PanGestureRecognizer(); recognizer.PanUpdated += recognizer_PanUpdated; GestureRecognizers.Add(recognizer); } #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Method ////////////////////////////////////////////////////////////////////////////////////////// Private #region 인식기 패닝 업데이트시 처리하기 - recognizer_PanUpdated(sender, e) /// <summary> /// 인식기 패닝 업데이트시 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void recognizer_PanUpdated(object sender, PanUpdatedEventArgs e) { switch(e.StatusType) { case GestureStatus.Running : Content.TranslationX = Math.Max(Math.Min(0, this.currentX + e.TotalX), -Math.Abs(Content.Width - Width )); Content.TranslationY = Math.Max(Math.Min(0, this.currentY + e.TotalY), -Math.Abs(Content.Height - Height)); break; case GestureStatus.Completed : this.currentX = Content.TranslationX; this.currentY = Content.TranslationY; break; } } #endregion } |
▶ MainPage.xaml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
<?xml version="1.0" encoding="utf-8" ?> <ContentPage x:Class="TestProject.MainPage" xmlns="http://schemas.microsoft.com/dotnet/2021/maui" xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" xmlns:local="clr-namespace:TestProject"> <local:PanContentView HorizontalOptions="Center" VerticalOptions="Center" WidthRequest="300" HeightRequest="300"> <Image Source="source.png" HorizontalOptions="Start" VerticalOptions="Start" WidthRequest="1920" HeightRequest="1280" /> </local:PanContentView> </ContentPage> |
TestProject.zip