移动端双击位移,单指旋转,双指缩放

本文探讨移动端应用中常见手势操作的实现,包括双击屏幕实现平移、单指滑动进行旋转以及双指捏合执行缩放的功能。这些手势为用户提供直观的交互体验,是现代移动应用开发不可或缺的部分。
摘要由CSDN通过智能技术生成

    private Camera camera = null;

    [HideInInspector]
    public float speed = 1f;

    private float angleHV, angleVV;
    [HideInInspector]
    public float smoothTime = 0.2f;
    float xSpeed;
    float ySpeed;
    private float lastDist = 0;
    private float curDist = 0;
    float angleHA = 45f;//Y轴旋转多少度
    float angleVA = 90f;//X轴旋转多少度
    private float curHAngle;
    private float curVAngle;
    private bool bMoving = false;
    private GameObject goSelected = null;
    bool roofOpen = false;

    void Start()
    {
        camera = this.GetComponentInChildren<Camera>();
    }

    IEnumerator MoveToPosition(Vector3 endPos)
    {
        while (this.transform.localPosition != endPos)
        {
            this.transform.localPosition = Vector3.MoveTowards(this.transform.localPosition, endPos, 10 * Time.deltaTime * 5f);//移动速度
            // print(xx);
            yield return 0;
        }
    }

    void LateUpdate()
    {

        if (Input.touchCount == 2)   //代表有两个手指
        {
            ySpeed = 0f;//禁止转动
            xSpeed = 0f;
            if (Input.GetTouch(1).phase == TouchPhase.Began)
            {

            }
            //缩放
            if (Input.GetTouch(0).phase == TouchPhase.Moved &&    //第一个手指               
               Input.GetTouch(1).phase == TouchPhase.Moved)            //第二个手指
            {
                Vector3 s1 = Input.GetTouch(0).position;         //第一个手指屏幕坐标
                Vector3 s2 = Input.GetTouch(1).position;         //第二个手指屏幕坐标
                curDist = Vector2.Distance(s1, s2);
                float delta = Mathf.Abs(curDist - lastDist);
                if (delta > 0.1f && delta < 80f)
                {
                    camera.fov += Time.deltaTime * (lastDist - curDist) * 1.5f;
                    camera.fov = Mathf.Clamp(camera.fov, 5, 80);

                }
                lastDist = curDist;
            }
        }
        else
        {
            if (Input.touchCount == 1)
            {
                if (EventSystem.current.IsPointerOverGameObject(Input.GetTouch(0).fingerId))
                {

                }
                else
                {

                    //拖动
                    if (Input.touches[0].phase == TouchPhase.Moved)
                    {
                        // 手指滑动时,要触发的代码 
                        //ySpeed = Mathf.Clamp( Input.GetAxis("Mouse X"),-5f,5f);    //手指水平移动的距离
                        //xSpeed = Mathf.Clamp( Input.GetAxis("Mouse Y"),-5f,5f);    //手指垂直移动的距离
                        angleHA += Input.GetTouch(0).deltaPosition.x * (360f / (float)Screen.width) * this.speed * 0.5f;
                        angleVA -= Input.GetTouch(0).deltaPosition.y * this.speed * (180f / (float)Screen.height) * 0.5f;
                        angleVA = Mathf.Clamp(angleVA, 10, 80f);//上下旋转角度
                        bMoving = true;
                    }

                    if (Input.touches[0].phase == TouchPhase.Began)
                    {
                        bMoving = false;
                    }

                    if (Input.touches[0].phase == TouchPhase.Ended)
                    {
                        if (bMoving) return;
                        RaycastHit hit;
                        Ray ray = camera.ScreenPointToRay(Input.touches[0].position);
                        bool isHit = Physics.Raycast(ray, out hit);
                        if (Input.touches[0].tapCount == 1)
                        {
                            if (isHit)
                            {
                                print(hit.collider.name);
                            }
                            if (hit.collider.name == "屋顶")//设备名称
                            {

                            }
                            else
                            {

                                //点中设备
                                if (hit.collider.tag == "SheBei")
                                {
                                    try
                                    {
                                        ///if (goSelected) goSelected.SetActive(false);
                                        ///goSelected = hit.transform.FindChild("Name").gameObject;
                                        ///goSelected.SetActive(true);

                                        /// HighlighterController hc = new HighlighterController();
                                        if (goSelected != null)
                                        {
                                            try
                                            {
                                                Destroy(goSelected.GetComponent<ConstantHighlighting>());
                                                Destroy(goSelected.GetComponent<Highlighter>());
                                            }
                                            catch { }
                                        }
                                        goSelected = hit.collider.gameObject;
                                        goSelected.AddComponent<ConstantHighlighting>();


                                    }
                                    catch (Exception exp)
                                    {
                                    }
                                }
                                else//取消选择
                                {

                                    if (goSelected != null)
                                    {
                                        try
                                        {
                                            Destroy(goSelected.GetComponent<ConstantHighlighting>());
                                            Destroy(goSelected.GetComponent<Highlighter>());
                                        }
                                        catch { }
                                    }
                                    goSelected = null;
                                }
                            }//end 设备
                        }

                        else
                        {//双击移动
                            if (Input.touches[0].tapCount == 2)
                            {

                                if (isHit)
                                {

                                    if (hit.collider.tag == "Dimian")//设备标签
                                    {
                                        StopCoroutine("MoveToPosition");//调用协程
                                        StartCoroutine("MoveToPosition", hit.point);
                                    }
                                    if (hit.collider.tag == "SheBei")//设备标签
                                    {
                                        StopCoroutine("MoveToPosition");//调用协程
                                        StartCoroutine("MoveToPosition", hit.collider.transform.position);
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
        #region 旋转
        this.curHAngle = Mathf.SmoothDamp(this.curHAngle, angleHA, ref this.angleHV, this.smoothTime);
        this.curVAngle = Mathf.SmoothDamp(this.curVAngle, angleVA, ref this.angleVV, this.smoothTime);
        Vector3 eulerAngles = transform.eulerAngles;
        transform.eulerAngles = new Vector3(this.curVAngle, this.curHAngle, eulerAngles.z);
        #endregion

    }

 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值