- Home /
how to pinch to zoom a ract image
if (Input.touchCount == 2 && imageRect.Contains(new Vector2(firstTouch.position.x, (Screen.height - firstTouch.position.y))) && imageRect.Contains(new Vector2(secondTouch.position.x, (Screen.height - secondTouch.position.y)))) { colorHolds = false; currentEvent = TouchEvent.Zooming; Vector2 firstTouchPrevPos = firstTouch.position - firstTouch.deltaPosition; Vector2 secondTouchPrevPos = secondTouch.position - secondTouch.deltaPosition; Vector2 Mid = (firstTouch.position + secondTouch.position)/2; //Vector2 Mid = (firstTouch.position+diff)/ 2;
float oldDistance = Vector2.Distance(firstTouchPrevPos, secondTouchPrevPos);
float newDistance = Vector2.Distance(firstTouch.position, secondTouch.position);
float scaleFactor = (newDistance / oldDistance);
if (scaleFactor > 0f)
{// scale image based on finger positions with a max zoomed postion.
if (imageRect.width >= origImgRect.width && imageRect.width < (3f * origImgRect.width))
imageRect.width *= (scaleFactor);
if (imageRect.height >= origImgRect.height && imageRect.height < (3f * origImgRect.height))
imageRect.height *= (scaleFactor);
Vector2 newPosZero = new Vector2(firstTouch.position.x - (firstTouchPrevPos.x - imageRect.x) * scaleFactor, firstTouch.position.y - (firstTouchPrevPos.y - imageRect.y) * scaleFactor);
Vector2 newPosOne = new Vector2(secondTouch.position.x - (secondTouchPrevPos.x - imageRect.x) * scaleFactor, secondTouch.position.y - (secondTouchPrevPos.y - imageRect.y) * scaleFactor);
//////////if (imageRect.height < (3f * origImgRect.height) && imageRect.height >= origImgRect.height && imageRect.width < (3f * origImgRect.width) && imageRect.width >= origImgRect.width)
//////////{
////////// imageRect.x = (newPosOne.x + newPosZero.x) / 2;
////////// imageRect.y = (newPosOne.y + newPosZero.y) / 2;
//////////}
}
Your answer
Follow this Question
Related Questions
How to zoom and pan when you "pinch/swipe across screen" (iPhone) 5 Answers
How Clamping orthographic camera between Ui Panel? 0 Answers
What's the best way to do pinch zoom? 1 Answer
Zooming cam by finger gestures 0 Answers
Pinch-zoom camera 2 Answers