Skip to content

Instantly share code, notes, and snippets.

@bardic bardic/swipe_zoom.cs Secret
Last active Feb 18, 2018

Embed
What would you like to do?
Unity image swipe and pinch zoom
private void Update()
{
if (!swipeBlocked)
{
if (Input.GetMouseButtonDown(0))
{
fp = Input.mousePosition;
lp = Input.mousePosition;
}
else
{
if (Input.GetMouseButtonUp(0))
{
lp = Input.mousePosition;
if (Mathf.Abs(lp.x - fp.x) > dragDistance)
{
OnSwipe(lp.x > fp.x);
}
}
}
}
if (Input.touchCount == 2)
{
swipeBlocked = true;
CancelInvoke("UnblockSwipe");
if (!pinchBegan)
{
pinchBegan = true;
startingScale = middleImage.transform.localScale.x;
startingTouchZero = Input.GetTouch(0);
startingTouchOne = Input.GetTouch(1);
}
Touch touchZero = Input.GetTouch(0);
Touch touchOne = Input.GetTouch(1);
float startingMag = (startingTouchZero.position - startingTouchOne.position).magnitude;
float mag = (touchZero.position - touchOne.position).magnitude;
float scale = (mag /startingMag) * startingScale;
if (scale > maxScale)
{
scale = maxScale;
}
if (scale < minScale)
{
scale = minScale;
}
middleImage.transform.localScale = new Vector3(scale, scale, 1);
Invoke("UnblockSwipe", 0.1f);
}
else
{
pinchBegan = false;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.
You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session.