Unity多指触摸UI跟随和缩放(缩放点在两指中间)

坑不是很多,直接放代码了。有问题联系QQ412484723,直接挂到UI上就可以用

  思路就是ui的坐标位置始终跟第一次按下去的位置的x距离和y距离保持一致,单指的时候直接是第一次按下去的坐标,双指的时候是当前帧的两个点位置中心。之所以用前一帧和当前帧的touch数量判断是为了避免更多点触摸造成位置不准确的问题

  在两指中间的缩放,我的思路是先按比例缩放,上一帧两手指的距离/上一帧的scale等于当前帧的两手指距离/scale,这样可以求出scale,记录上一帧两手指中心位置在要缩放的ui下的坐标和ui父物体下的坐标,然后计算当前两手指中心位置在ui下的坐标和父物体下的坐标,用ui的pos加上这两个值就行了。还有一种方法,按下去的时候记录两个手指在ui中坐标,下一帧的时候两个手指在屏幕的坐标换算到ui本地坐标也是上两个值,根据前后四个点反推坐标轴的旋转位移和缩放,这里给出第一种的实现。(canvas用的camera模式,这里直接用的maincamera,具体的应该getcomponentinparent<canvas>().worldcamera这样获取camera)

链接: https://pan.baidu.com/s/1iS_0CltNGaY3z7bSYIOwGA 提取码: hz9g 复制这段内容后打开百度网盘手机App,操作更方便哦


using UnityEngine;

using UnityEngine.Experimental.Rendering;

using UnityEngine.UI;

public class PinchZoom : MonoBehaviour

{

    private bool _isMousePressed;

    public float MaxScale = 6.0f;

    public float MinScale = 0.6f;

    private Vector2 _midPoint = new Vector2(0, 0);

    private Vector2 _start1;

    private Vector2 _start2;

    private Vector2 _startMid;

    private Vector2 _touchScale;

    private Vector2 _touchPos;

    private Vector2 _startMouse;

    private Vector2 pp3;

    private Vector2 pp4;

    private float _touchLength;

    Vector2 aPos1 = new Vector2();

    Vector2 aPos2 = new Vector2();

    private float _x;

    private float _y;

    private float _pressTime;

    //上一帧touch数量

    private int _priorInputCount;

    private RectTransform _t;

    void Start()

    {

        _t = (RectTransform)transform;

        var c = GetComponentInParent<Canvas>();

        Application.targetFrameRate = 60;

        Debug.Log(c.worldCamera.name);

    }

    void Update()

    {

        //CheckZoomForMobile();

        CheckMovingForMobile();

    }

    private void CheckMovingForMobile()

    {

#if UNITY_EDITOR_WIN

        if (Input.GetMouseButtonDown(0))

            _isMousePressed = true;

        else if (Input.GetMouseButtonUp(0))

            _isMousePressed = false;

        if (_isMousePressed)

            _pressTime += Time.deltaTime;

        else

            _pressTime = 0;

        //Zoom out

        if (Input.GetAxis("Mouse ScrollWheel") > 0)

        {

            var pos1 = ScreenToAnchorPosition(Input.mousePosition, _t);

            if (_t.localScale.x < MaxScale)

                _t.localScale *= 1.1f;

            var pos2 = ScreenToAnchorPosition(Input.mousePosition, _t);

            Debug.Log(pos1 + "/" + pos2);

            _t.anchoredPosition += (pos2 - pos1) * _t.localScale.x;

        }

        //Zoom in 

        if (Input.GetAxis("Mouse ScrollWheel") < 0)

        {

            var pos1 = ScreenToAnchorPosition(Input.mousePosition, _t);

            if (_t.localScale.x > MinScale)

                _t.localScale *= 0.9f;

            var pos2 = ScreenToAnchorPosition(Input.mousePosition, _t);

            _t.anchoredPosition += (pos2 - pos1) * _t.localScale.x;

        }

        if (_pressTime > 0.05f)

        {

            var x = Input.mousePosition.x;

            var y = Input.mousePosition.y;

            _t.anchoredPosition += new Vector2(x - _x, y - _y);

        }

        _x = Input.mousePosition.x;

        _y = Input.mousePosition.y;

#endif

        //text1.text=_midPoint.ToString();

        if (Input.touchCount == 1)

        {

            var aPos = ScreenToAnchorPosition(Input.GetTouch(0).position, transform.parent.transform as RectTransform);

            //第一个点按下或者其余点离开的时候

            if (_priorInputCount != 1)

            {

                _x = aPos.x - _t.anchoredPosition.x;

                _y = aPos.y - _t.anchoredPosition.y;

            }

            _midPoint = aPos;

            _t.anchoredPosition = new Vector2(_midPoint.x - _x, _midPoint.y - _y);

        }

        if (Input.touchCount == 2)

        {

            //第二个点按下或者其余点离开的时候

            var iPos1 = Input.GetTouch(0).position;

            var iPos2 = Input.GetTouch(1).position;

            _start1 = ScreenToAnchorPosition(iPos1, _t);

            _start2 = ScreenToAnchorPosition(iPos2, _t);

            if (_priorInputCount != 2)

            {

                _touchScale = _t.localScale;

                _touchPos = _t.anchoredPosition;

                _touchLength = Vector2.Distance(iPos1, iPos2);

            }

            var pos1 = new Vector2((_start1.x + _start2.x) / 2, (_start1.y + _start2.y) / 2);

            var curLength = Vector2.Distance(iPos1, iPos2);

            var scale = curLength * _touchScale.x / _touchLength;

            scale = Mathf.Clamp(scale, MinScale, MaxScale);

            _t.localScale = new Vector3(scale, scale, 1);

            var p1 = ScreenToAnchorPosition(Input.GetTouch(0).position, _t);

            var p2 = ScreenToAnchorPosition(Input.GetTouch(1).position, _t);

            var pos2 = new Vector2((p1.x + p2.x) / 2, (p1.y + p2.y) / 2);

            _t.anchoredPosition += (pos2 - pos1) * scale;

            if(_priorInputCount==2)

            {

                var pp1 = ScreenToAnchorPosition(iPos1, _t.parent.transform as RectTransform);

                var pp2 = ScreenToAnchorPosition(iPos2, _t.parent.transform as RectTransform);

                var curMid = new Vector2((pp1.x + pp2.x) / 2, (pp1.y + pp2.y) / 2);

                var priorMid=new Vector2((pp3.x + pp4.x) / 2, (pp3.y + pp4.y) / 2);

                _t.anchoredPosition += curMid - priorMid;

            }

            pp3 = ScreenToAnchorPosition(iPos1, _t.parent.transform as RectTransform);

            pp4 = ScreenToAnchorPosition(iPos2, _t.parent.transform as RectTransform);

        }

        _priorInputCount = Input.touchCount;

    }

    Vector2 ScreenToAnchorPosition(Vector2 pos, RectTransform rT)

    {

        Vector2 position;

        RectTransformUtility.ScreenPointToLocalPointInRectangle(rT, pos, Camera.main, out position);

        return position;

    }

}

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 160,026评论 4 364
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 67,655评论 1 296
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 109,726评论 0 244
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 44,204评论 0 213
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 52,558评论 3 287
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 40,731评论 1 222
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 31,944评论 2 314
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 30,698评论 0 203
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 34,438评论 1 246
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 30,633评论 2 247
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 32,125评论 1 260
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 28,444评论 3 255
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 33,137评论 3 238
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 26,103评论 0 8
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 26,888评论 0 197
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 35,772评论 2 276
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 35,669评论 2 271

推荐阅读更多精彩内容