手指缩放

Posted 81192

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了手指缩放相关的知识,希望对你有一定的参考价值。

unity 实现两个手指缩放功能有很多插件,比如easyTouch、FingerGestures、TouchKit等这些均为功能比较多插件,有时候单纯为了一个手指缩放的单一功能又没有必要导入插件,所以一下为代码,缩放通过控制scale来实现

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class VideoScreenShrink : MonoBehaviour {
    public float shrinkScale = 0.5f;                       //缩放速度

    private Vector2 initPostion1;
    private Vector2 initPostion2;

    private Vector2 tempPostion1;
    private Vector2 tempPostion2;

    private bool isInited;

    // Use this for initialization
    void Start()
    {
        
    }

    // Update is called once per frame
    void Update()
    {
        if (Input.touchCount > 1)
        {
            if(!isInited)
            {
                initPostion1 = Input.GetTouch(0).position;
                initPostion2 = Input.GetTouch(1).position;
                isInited = true;
            }
         
            if(Input.GetTouch(0).phase == TouchPhase.Moved||
                Input.GetTouch(1).phase == TouchPhase.Moved)
            {
                tempPostion1 = Input.GetTouch(0).position;
                tempPostion2 = Input.GetTouch(1).position;

                Vector3 iniScale = GetComponent<RectTransform>().localScale;

                float initDis = Vector2.Distance(initPostion1, initPostion2);
                float tempDis = Vector2.Distance(tempPostion1, tempPostion2);
                float offset = tempDis - initDis;
                float offsetScale = (offset / initDis) * shrinkScale;
                Vector3 scale = new Vector3(iniScale.x + offsetScale,iniScale.y + offsetScale,1);

                GetComponent<RectTransform>().localScale = new Vector3(Mathf.Clamp(scale.x, 0.3f, 1),
                    Mathf.Clamp(scale.y, 0.3f, 1), 1);                                                  //比例控制在0.3·1,可手动修改
            }
            else
            {
                isInited = false;
            }
        }
    }
}

 

以上是关于手指缩放的主要内容,如果未能解决你的问题,请参考以下文章

iPhone SDK 用 2 根手指缩放/调整图像大小!!帮助

在 webview_flutter 中启用捏合和缩放,在哪里添加代码片段 [this.webView.getSettings().setBuiltInZoomControls(true);]

在 Safari/Mail 等中模拟 iOS 缩放工具

SWFLoader 上的 Actionscript 3 和 Flex 4 缩放手势

如何启用 google maps android api 以实现平滑的两指缩放?

覆盖缩放 swipetorefresh - Android