Created
January 22, 2018 23:09
-
-
Save RomaRudyak/2e40070ba0bc6bde74af3e0a25f4dde4 to your computer and use it in GitHub Desktop.
Draft full screen image viewer
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class ImageViewer : NControlView | |
{ | |
private Image _image; | |
private NGraphics.Point _startPoint; | |
public ImageViewer() | |
{ | |
IsClippedToBounds = true; | |
_image = new Image(); | |
_image.InputTransparent = true; | |
Content = _image; | |
} | |
public static BindableProperty SourceProperty = | |
BindableProperty.Create(nameof(Source), | |
typeof(ImageSource), | |
typeof(ImageViewer), | |
default(ImageSource), | |
BindingMode.TwoWay, | |
propertyChanged: (bindable, oldValue, newValue) => | |
{ | |
var ctrl = (ImageViewer)bindable; | |
ctrl.Source = (ImageSource)newValue; | |
}); | |
public event Action<object, NGraphics.Point> OnTapped; | |
private bool _isMoved; | |
public ImageSource Source | |
{ | |
get { return (ImageSource)GetValue(SourceProperty); } | |
set | |
{ | |
SetValue(SourceProperty, value); | |
_image.Source = value; | |
} | |
} | |
public override bool TouchesBegan(System.Collections.Generic.IEnumerable<NGraphics.Point> points) | |
{ | |
base.TouchesBegan(points); | |
_startPoint = points.FirstOrDefault(); | |
_startPoint.X -= _image.TranslationX; | |
_startPoint.Y -= _image.TranslationY; | |
_isMoved = false; | |
return true; | |
} | |
public override bool TouchesMoved(System.Collections.Generic.IEnumerable<NGraphics.Point> points) | |
{ | |
base.TouchesMoved(points); | |
var newPoint = points.FirstOrDefault(); | |
var diff = new NGraphics.Point(newPoint.X - _startPoint.X, newPoint.Y - _startPoint.Y); | |
_isMoved = _isMoved | |
|| (_image.TranslationX != diff.X | |
&& _image.TranslationY != diff.Y); | |
_image.TranslationX = diff.X; | |
_image.TranslationY = diff.Y; | |
return true; | |
} | |
public override bool TouchesEnded(System.Collections.Generic.IEnumerable<NGraphics.Point> points) | |
{ | |
base.TouchesEnded(points); | |
if (!_isMoved) | |
{ | |
OnTapped?.Invoke(this, points.First()); | |
} | |
_isMoved = false; | |
return true; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment