• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by moaazafzal1111 · Mar 28, 2019 at 02:10 PM · zoomrectpinch

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;
             //////////}

         }
Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

0 Replies

· Add your reply
  • Sort: 

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

102 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges