当前位置:   article > 正文

unity 使用Touch点击跟随移动以及放大缩小图片_unity 图片的放大与查看

unity 图片的放大与查看
//图片集合
public List<Sprite> TE = new List<Sprite>(); 
//图片物体
public GameObject a;

bool BA = false;

int I = 0;


// Start is called before the first frame update
void Start()
{
    a.GetComponent<Image>().sprite = TE[0];
}
float flo;

float flo1;

float AB=0;

Vector3 trv3;

Vector2 transv3;
// Update is called once per frame
void Update()
{
    //若单指触屏
    if (Input.touchCount>0&&Input.touchCount<=1)
    {
        //获取touch
        Touch TO = Input.GetTouch(0);

        switch (TO.phase)
        {
            //刚接触屏幕 获取现在的大小和位置
            case TouchPhase.Began:
                trv3 = a.transform.localScale;
                transv3 = a.GetComponent<RectTransform>().anchoredPosition;
                break;
                //若手指移动
            case TouchPhase.Moved:
                //TO.rawPosition是刚接触屏幕时坐标 刚接触屏幕时坐标与现在的坐标做比较 求出差值
                Vector2 v2 = TO.rawPosition - TO.position;
                //若此时image坐标没有超出屏幕范围 当image坐标为(0,0)时在屏幕中间
                if (a.GetComponent<RectTransform>().anchoredPosition.x>-(Screen.width/2)&& a.GetComponent<RectTransform>().anchoredPosition.x < Screen.width / 2 && a.GetComponent<RectTransform>().anchoredPosition.y < Screen.height/2&& a.GetComponent<RectTransform>().anchoredPosition.y >-Screen.height / 2)
                {
                    //跟随手指移动
                    a.GetComponent<RectTransform>().anchoredPosition -= v2 / 100;
                }
                //下面是若超出了屏幕范围 则赋值于屏幕边缘
                else if (a.GetComponent<RectTransform>().anchoredPosition.x <= -Screen.width)
                {
                    a.GetComponent<RectTransform>().anchoredPosition = new Vector2(-Screen.width+1, a.GetComponent<RectTransform>().anchoredPosition.y);
                }
                else if (a.GetComponent<RectTransform>().anchoredPosition.x >= Screen.width)
                {
                    a.GetComponent<RectTransform>().anchoredPosition = new Vector2(Screen.width-1, a.GetComponent<RectTransform>().anchoredPosition.y);
                }
                else if (a.GetComponent<RectTransform>().anchoredPosition.y >= Screen.height)
                {
                    a.GetComponent<RectTransform>().anchoredPosition = new Vector2(a.GetComponent<RectTransform>().anchoredPosition.x, Screen.height-1);
                }
                else if (a.GetComponent<RectTransform>().anchoredPosition.y <= -Screen.height)
                {
                    a.GetComponent<RectTransform>().anchoredPosition = new Vector2(a.GetComponent<RectTransform>().anchoredPosition.x, -Screen.height+1);
                }

                break;
            case TouchPhase.Stationary:
                break;
                //手指离开屏幕
            case TouchPhase.Ended:
                //若大小和位置没有变化 说明只是点击屏幕  则切换image图片
                if (a.transform.localScale == trv3&&a.GetComponent<RectTransform>().anchoredPosition==transv3)
                {
                    I++;
                    if (I < TE.Count)
                    {
                        a.GetComponent<Image>().sprite = TE[I];
                        a.GetComponent<Image>().SetNativeSize();
                        a.GetComponent<RectTransform>().sizeDelta = new Vector2(a.GetComponent<RectTransform>().rect.width / 50, a.GetComponent<RectTransform>().rect.height / 50);
                    }
                    else
                    {
                        I = 0;
                        a.GetComponent<Image>().sprite = TE[I];
                        a.GetComponent<Image>().SetNativeSize();
                        a.GetComponent<RectTransform>().sizeDelta = new Vector2(a.GetComponent<RectTransform>().rect.width / 50, a.GetComponent<RectTransform>().rect.height / 50);
                    }
                }
                    break;
            case TouchPhase.Canceled:
                break;
            default:
                break;
        }
    }
    //若俩指触屏  则放大缩小图片
  else  if (Input.touchCount>1)
    {
        //获取指头1
        Touch to = Input.GetTouch(0);
        //获取指头2
        Touch to1 = Input.GetTouch(1);

        switch (to.phase)
        {
            //若指头1在移动  若放大缩小图片
            case TouchPhase.Moved:

                BA = true;
              break;
                //若停止移动 则取消放大缩小
            case TouchPhase.Stationary:
                BA = false;
              break;
        }
        switch (to1.phase)
        {
            case TouchPhase.Began:
                //获取指头1和指头2之间的初始坐标差值
                AB = Mathf.Abs(to.rawPosition.x - to1.rawPosition.x);
                break;
            case TouchPhase.Moved:
                BA = true;
              break;
            case TouchPhase.Stationary:
                BA = false;
              break;
        }
        //指头1和指头2现在的坐标差值
        float v2 = Mathf.Abs(to.position.x - to1.position.x);
        //若有指头移动
        if (BA==true)
        {
            //若现在的坐标差值大于之前的坐标差值 则放大图片
            if (v2 > AB&&a.transform.lossyScale.x<=100)
            {
                a.transform.localScale += new Vector3(0.1f,0.1f,0.1f);
                //现在的坐标差值赋值给AB
                AB = v2;
            }
            //若现在的坐标差值小于之前的坐标差值  则缩小图片
            else if (v2 < AB && a.transform.lossyScale.x >= 1f)
            {
                a.transform.localScale -= new Vector3(0.1f,0.1f,0.1f);
                AB = v2;
            }
        }
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117
  • 118
  • 119
  • 120
  • 121
  • 122
  • 123
  • 124
  • 125
  • 126
  • 127
  • 128
  • 129
  • 130
  • 131
  • 132
  • 133
  • 134
  • 135
  • 136
  • 137
  • 138
  • 139
  • 140
  • 141
  • 142
  • 143
  • 144
  • 145
  • 146
  • 147
  • 148
  • 149
  • 150
  • 151
  • 152
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/你好赵伟/article/detail/107915
推荐阅读
相关标签
  

闽ICP备14008679号