[C#/MAUI/.NET6] PanGestureRecognizer 클래스 : PanUpdated 이벤트를 사용해 패닝 처리하기 (기능 개선)
C#/MAUI 2022. 6. 18. 00:16728x90
반응형
728x170
▶ PanContentView.cs
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
}
728x90
▶ MainPage.xaml
<?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>
728x90
반응형
그리드형(광고전용)
댓글을 달아 주세요