Created
May 11, 2015 21:37
-
-
Save lineker/c77b5c5fdf404c04d856 to your computer and use it in GitHub Desktop.
This file contains hidden or 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 InteractiveView : UIView | |
{ | |
public bool IsDragDisabled { | |
get; | |
set; | |
} | |
public bool IsPinchDisabled { | |
get; | |
set; | |
} | |
public bool IsRotateDisabled { | |
get; | |
set; | |
} | |
private CGRect originalImageFrame = CGRect.Empty; | |
private nfloat _scale; | |
private nfloat _angle; | |
public InteractiveView (IntPtr handle) | |
: base(handle) | |
{ | |
} | |
public InteractiveView (CGRect frame) | |
: base(frame) | |
{ | |
originalImageFrame = frame; | |
var panGesture = new UIPanGestureRecognizer (); | |
panGesture.AddTarget (() => HandleDrag(panGesture)); | |
var pinchGesture = new UIPinchGestureRecognizer (); | |
pinchGesture.AddTarget (() => HandlePinch(pinchGesture)); | |
var rotateGesture = new UIRotationGestureRecognizer (); | |
rotateGesture.AddTarget (() => HandleRotate(rotateGesture)); | |
panGesture.ShouldRecognizeSimultaneously = (gesture1, gesture2) => gesture2 == pinchGesture || gesture2 == rotateGesture; | |
pinchGesture.ShouldRecognizeSimultaneously = (gesture1, gesture2) => gesture2 == panGesture || gesture2 == rotateGesture; | |
rotateGesture.ShouldRecognizeSimultaneously = (gesture1, gesture2) => gesture2 == panGesture || gesture2 == pinchGesture; | |
this.AddGestureRecognizer(pinchGesture); | |
this.AddGestureRecognizer(panGesture); | |
this.AddGestureRecognizer(rotateGesture); | |
} | |
private void HandleDrag(UIPanGestureRecognizer recognizer) | |
{ | |
if (IsDragDisabled) | |
return; | |
// If it's just began, cache the location of the image | |
if (recognizer.State == UIGestureRecognizerState.Began) | |
{ | |
originalImageFrame = this.Frame; | |
} | |
// Move the image if the gesture is valid | |
if (recognizer.State != (UIGestureRecognizerState.Cancelled | UIGestureRecognizerState.Failed | |
| UIGestureRecognizerState.Possible)) | |
{ | |
// Move the image by adding the offset to the object's frame | |
CGPoint offset = recognizer.TranslationInView(this); | |
CGRect newFrame = originalImageFrame; | |
newFrame.Offset(offset.X, offset.Y); | |
this.Frame = newFrame; | |
} | |
} | |
private void HandlePinch (UIPinchGestureRecognizer pinchGesture) | |
{ | |
if (IsPinchDisabled) | |
return; | |
_scale = pinchGesture.Scale; | |
this.Transform = MakeTransform(); | |
} | |
private void HandleRotate (UIRotationGestureRecognizer rotateGesture) | |
{ | |
if (IsRotateDisabled) | |
return; | |
_angle = rotateGesture.Rotation; | |
this.Transform = MakeTransform(); | |
} | |
private CGAffineTransform MakeTransform() | |
{ | |
var transform = CGAffineTransform.MakeIdentity(); | |
transform.Scale(_scale, _scale); | |
transform.Rotate(_angle); | |
return transform; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment