zoukankan      html  css  js  c++  java
  • NGUI 滑动翻页效果

    using UnityEngine;
    using System.Collections;
       
    public class PageView : MonoBehaviour
    {
        const int ITEM_NUM = 2;        //总页数
        const int PAGE_WIDTH = 2048;    //页宽
        const float DRAG_SPEED = 0.5f;    //翻页时间
        const int DRAG_OFFECT = 30;    //滑动的起点和终点的差需大于这个数才能触发翻页效果
            
        float beganX = 0;    
        float beganY = 0;            //鼠标按下的坐标
    
    
        int curIndex = 1;            //当前页数,默认为第一页
    
        bool isPlay = false;        //是否正在翻页
        bool isPress = false;        //鼠标是否按下
        bool isPageFoot = false;    //当前是否处于页尾
        bool isHomePage = true;        //当前是否处于首页
    
        string left = "left";        //左滑动画的name
        string right = "right";        //右滑动画的name
        
        GameObject[] Item_Objects;
    
       // Use this for initialization
       void Start ()
       {
            this.Init ();
       }
    
        void Init()
        {
            Item_Objects = new GameObject[ITEM_NUM];
    
            for(int i = 1; i <= ITEM_NUM; ++i)
            {
                Transform trans = this.transform.FindChild("item" + i);
    
                if(trans)
                {
                    GameObject spr = trans.transform.FindChild("Background").gameObject;
                    spr.AddComponent<UIEventListener>();
                    UIEventListener.Get(spr.gameObject).onPress = OnPressEvent;
                }
                Item_Objects[i - 1] = trans.gameObject;
            }
        }
    
        //鼠标按下事件监听
        void OnPressEvent(GameObject obj,bool isDown)
        {
            float endX;
            float endY;
            if (isDown) 
            {
                beganX = UICamera.lastTouchPosition.x;
                beganY = UICamera.lastTouchPosition.y;
    
                isPress = true;
            } else 
            {
                endX = UICamera.lastTouchPosition.x;
                endY = UICamera.lastTouchPosition.y;
    
                if (isPress) 
                {
                    if(isPlay == false)
                    {
                        if(endX - beganX > DRAG_OFFECT)
                        {
                            if(isHomePage == false)
                            {
                                RightDrag();
                            }
                        }else if(endX - beganX < DRAG_OFFECT){
                            if(isPageFoot == false)
                            {
                                LeftDrag();
                            }
                        }
                    }
                }
                isPress = false;
            }
        }
    
        //向左滑
        void LeftDrag()
        {
            isPlay = true;
    
            float x = this.transform.localPosition.x - PAGE_WIDTH;
    
            TweenPosition leftTween = TweenPosition.Begin (this.gameObject,DRAG_SPEED,new Vector3(x,0,0));
            leftTween.method = UITweener.Method.EaseInOut;
            leftTween.callWhenFinished = "callback";
            leftTween.name = left;
            leftTween.Reset ();
    
        }
    
        //向右滑
        void RightDrag()
        {
            isPlay = true;
    
            float x = this.transform.localPosition.x + PAGE_WIDTH;
    
            TweenPosition rightTween = TweenPosition.Begin (this.gameObject,DRAG_SPEED,new Vector3(x,0,0));
            rightTween.method = UITweener.Method.EaseInOut;
            rightTween.callWhenFinished = "callback";
            rightTween.name = right;
            rightTween.Reset ();
    
        }
    
        //动画结束的回调函数
        void callback(UITweener tween)
        {
            isPlay = false;
    
            if (tween.name == left) 
            {
                curIndex ++;
            } else if (tween.name == right) 
            {
                curIndex --;
            }
            if (curIndex == 1) 
            {
                isHomePage = true;
            }else
            {
                isHomePage = false;
            }
            if(curIndex == ITEM_NUM){
                isPageFoot = true;
            }else
            {
                isPageFoot = false;
            }
    
        }
        
    }
  • 相关阅读:
    查询同一表格中姓名相同但身份证号不同的记录
    Liunx常用命令
    判断当前移动端是Android、还是ios、还是微信
    mybatis 返回值问题
    log4j2+mybaits 打印sql操作语句
    java日期格式问题
    eachart图表100px大小原因,及处理办法
    springboot中的默认数据库连接池HikariDataSource
    SpringBoot中logback.xml使用application.yml中属性
    linux 下的vi vim快捷键,命令总结
  • 原文地址:https://www.cnblogs.com/Smart-Du/p/4613377.html
Copyright © 2011-2022 走看看