• 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 /
This post has been wikified, any user with enough reputation can edit it.
avatar image
0
Question by Zenthurion · Feb 23, 2015 at 12:25 AM · uitouchzoom

How to add touch zoom to a ScrollRect without the contents jumping around?

I have written a simple script to zoom in and out of a ScrollRect by using two fingers. However, if I remove my fingers at different times, the contents of the ScrollRect will center on the remaining finger, causing everything to jump away from where I want it to be. If I put down the second finger again, it centers back to the middle-point of my two fingers. Is there any way to work around this that isn't writing my own scroll rect class?

Thanks!

Here is my code, in case it's relevant:

 private void Zoom(float direction, bool pinching) {
         if(direction > 0) {
             if(scrollPanel.transform.localScale.x < maxScale) {
                 scrollPanel.transform.localScale += Vector3.one * 0.1f * 1 * (pinching ? 0.3f : 1);
             } else {
                 scrollPanel.transform.localScale = Vector3.one * maxScale;
             }
         } else if(direction < 0) {
             if(scrollPanel.transform.localScale.x > minScale) {
                 scrollPanel.transform.localScale += Vector3.one * 0.1f * -1 * (pinching ? 0.3f : 1);
             } else {
                 scrollPanel.transform.localScale = Vector3.one * minScale;
             }
         } 
     }
 
     private void CheckPinch() {
         if(Input.touchCount == 2) {
             Touch tOne = Input.GetTouch(0);
             Touch tTwo = Input.GetTouch(1);
 
             Vector2 tOnePrevPos = tOne.position - tOne.deltaPosition;
             Vector2 tTwoPrevPos = tTwo.position - tTwo.deltaPosition;
 
             float movementThreshold = 1f;
 
             if(tOne.deltaPosition.magnitude > movementThreshold && tTwo.deltaPosition.magnitude > movementThreshold) {
                 float prevDist = (tOnePrevPos - tTwoPrevPos).magnitude;
                 float currDist = (tOne.position - tTwo.position).magnitude;
 
                 float change = currDist - prevDist;
                 Zoom(change, true);
             }
             scrollRect.StopMovement();
         }
     }
Comment
Add comment · Show 3
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
avatar image IvovdMarel · Feb 23, 2015 at 12:56 AM 1
Share

Have you considered disabling the horizontal and/or vertical movement of the scrollrect when two fingers are down and only enabling it when no fingers are down?

That way, only when you fully release two fingers and then touch down with one finger again, you will be able to scroll.

avatar image Zenthurion · Feb 23, 2015 at 06:48 PM 0
Share

Thanks, it works reasonably well! No more jumping around, anyways.

avatar image Wang ZongJun · Mar 17, 2016 at 03:20 PM 0
Share

Now, i do the same thing.Can you give me a more detail information how you resolved?

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

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

EventSystem not detecting all UI elements 1 Answer

How to get the fingerID (Touch) that is pressing a specific UI button. 0 Answers

Ui sidescroller 2d touch ? 0 Answers

Touch goes through UI button 1 Answer

UI Touch Events not working? 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges